Python 2.7 多处理池挂起在jupyter笔记本中
我有一个非常简单的脚本,如下所示:Python 2.7 多处理池挂起在jupyter笔记本中,python-2.7,multiprocessing,Python 2.7,Multiprocessing,我有一个非常简单的脚本,如下所示: import multiprocessing as multi def call_other_thing_with_multi(): P = multi.Pool(3) P.map(other_thing, range(0,5)) P.join() def other_thing(arg): print(arg) return arg**2. call_other_thing_with_multi() 当我调用
import multiprocessing as multi
def call_other_thing_with_multi():
P = multi.Pool(3)
P.map(other_thing, range(0,5))
P.join()
def other_thing(arg):
print(arg)
return arg**2.
call_other_thing_with_multi()
当我调用它时,我的代码永久挂起。这是在使用python 2.7的windows上实现的
谢谢你的指导 根据,您需要在join()之前调用close()
:
编辑:最好使用上下文管理器,不要忘记调用close()
:
这其实也hangs@jasonm您运行的是完全相同的代码吗?我使用的是Python3.6,但应该没有什么不同。我添加了带有上下文管理器的版本。是,运行相同的代码。如图所示,在py27中它不起作用,上下文也不起作用。我的问题措词不当,你的解决办法确实有效。
import multiprocessing as multi
def call_other_thing_with_multi():
P = multi.Pool(3)
P.map(other_thing, range(0,5))
P.close() # <-- calling close before P.join()
P.join()
print('END')
def other_thing(arg):
print(arg)
return arg**2.
call_other_thing_with_multi()
0
1
2
3
4
END
def call_other_thing_with_multi():
with multi.Pool(3) as P:
P.map(other_thing, range(0,5))
print('END')