简体   繁体   English

同时运行 jupyter notebook 单元?

[英]Simultaneously running jupyter notebook cells?

I am trying to run 2 jupyter notebook cells simultaneously.我正在尝试同时运行 2 个 jupyter 笔记本单元。

First, I define首先,我定义

import time
list1 = ["a"]

Then i want to run the following然后我想运行以下

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

While the cell above is running, i want to be able to update list1 and see the updated output in the cell above ie run another cell当上面的单元格运行时,我希望能够更新 list1 并在上面的单元格中看到更新的 output 即运行另一个单元格

list1.append("b")

I have looked into using ipyparallel package and tried to use this answer and documentation with no success Is there a way to run multiple cells simultaneously in IPython notebook?我已经研究过使用 ipyparallel package 并尝试使用此答案和文档但没有成功有没有办法在 IPython 笔记本中同时运行多个单元?

Does anyone know how to do this?有谁知道如何做到这一点?

Thanks谢谢

You can use multiprocess module in order to run parallel cells您可以使用multiprocess模块来运行并行单元

You probably need to use multiprocess Manager 's list and Value in order to get shared variables between threads您可能需要使用 multiprocess Manager的 list 和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()

To update list1 in another cell, you can try something like mentioned above要更新另一个单元格中的list1 ,您可以尝试上面提到的方法

list1.append("b")

To update go , you can set its value property to be the value you want要更新go ,您可以将其value属性设置为您想要的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM