我尝试在here处测试示例,将其从线程更改为多处理。
在jupyter笔记本中运行此示例(原始示例),将显示一个进度条,该条会在一段时间内填满。
import threading
from IPython.display import display
import ipywidgets as widgets
import time
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
def work(progress):
total = 100
for i in range(total):
time.sleep(0.2)
progress.value = float(i+1)/total
thread = threading.Thread(target=work, args=(progress,))
display(progress)
thread.start()
如果我将代码更改为多处理,则会出现进度条,但不会填满:
import multiprocessing as mp
from IPython.display import display
import ipywidgets as widgets
import time
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
def work(progress):
total = 100
for i in range(total):
time.sleep(0.2)
progress.value = float(i+1)/total
p = mp.Process(target = work, args=(progress,))
display(progress)
p.start()
如果我通过在worker循环中添加print(i)
来修改上述内容,进度条将神奇地开始填充。
import multiprocessing as mp
from IPython.display import display
import ipywidgets as widgets
import time
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
def work(progress):
total = 100
for i in range(total):
print(i)
time.sleep(0.2)
progress.value = float(i+1)/total
p = mp.Process(target = work, args=(progress,))
display(progress)
p.start()
这是一个错误还是有一些我根本不了解的基本信息?
OS是Ubuntu 18.04
$jupyter --version
4.4.0
import sys; sys.version
3.7.0 (default, Oct 15 2018, 10:26:13) \n[GCC 7.3.0]