【问题标题】:Simultaneously running jupyter notebook cells?同时运行 jupyter notebook 单元?
【发布时间】:2021-03-26 22:05:08
【问题描述】:

我正在尝试同时运行 2 个 jupyter notebook 单元。

首先,我定义

import time
list1 = ["a"]

然后我想运行以下

go = True
while go==True:
    time.sleep(1)
    print(list1)

当上面的单元格运行时,我希望能够更新 list1 并在上面的单元格中查看更新后的输出,即运行另一个单元格

list1.append("b")

我已经研究过使用 ipyparallel 包并尝试使用这个答案和文档但没有成功 Is there a way to run multiple cells simultaneously in IPython notebook?

有人知道怎么做吗?

谢谢

【问题讨论】:

    标签: python parallel-processing jupyter-notebook


    【解决方案1】:

    您可以使用multiprocess 模块来运行并行单元

    您可能需要使用多进程Manager 的列表和Value 来获取线程之间的共享变量

    import time
    from multiprocess import Manager, Process, Value
    
    manager = Manager()
    list1 = manager.list(["a"])
    go = Value('b', True)
    
    def worker(list1, go):
        while go.value:
            time.sleep(1)
            print(list1)
        return
    
    p = Process(target=worker, args=(list1, go))
    p.start()
    

    要在另一个单元格中更新list1,您可以尝试上述方法

    list1.append("b")
    

    要更新go,您可以将其value 属性设置为您想要的值

    go.value = True # This will output list1 in worker
    go.value = False # This exit worker while loop
    

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多