multiprocessing vs threading in jupyter notebook

≯℡__Kan透↙ 提交于 2021-02-10 20:22:54

问题


I trying to test the example at here changing it from threading to multiprocessing.

Running this (the original example) in jupyter notebook, will display a progress bar that fills up in some time.

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()

If I change the code to multiprocessing, the progress bar appears, but does not fill up:

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()

If I modify the above by adding a print(i) in the workers loop, the progress bar magically starts to fill up.

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()

Is this a bug or is there something fundamental I am not getting here?

Edit (system info)

OS is 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]

来源:https://stackoverflow.com/questions/54335524/multiprocessing-vs-threading-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!