同时 运行 jupyter 笔记本单元?
Simultaneously running jupyter notebook cells?
我正在尝试同时 运行 2 个 jupyter notebook 单元格。
首先我定义
import time
list1 = ["a"]
那我要运行下面的
go = True
while go==True:
time.sleep(1)
print(list1)
虽然上面的单元格是 运行ning,但我希望能够更新 list1 并在上面的单元格中查看更新后的输出,即 运行 另一个单元格
list1.append("b")
我研究过使用 ipyparallel 包并尝试使用此答案和文档但没有成功
Is there a way to run multiple cells simultaneously in IPython notebook?
有人知道怎么做吗?
谢谢
您可以使用 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
我正在尝试同时 运行 2 个 jupyter notebook 单元格。
首先我定义
import time
list1 = ["a"]
那我要运行下面的
go = True
while go==True:
time.sleep(1)
print(list1)
虽然上面的单元格是 运行ning,但我希望能够更新 list1 并在上面的单元格中查看更新后的输出,即 运行 另一个单元格
list1.append("b")
我研究过使用 ipyparallel 包并尝试使用此答案和文档但没有成功 Is there a way to run multiple cells simultaneously in IPython notebook?
有人知道怎么做吗?
谢谢
您可以使用 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