我有一个非常简单的脚本,如下:
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上。
感谢您的指导!
答案 0 :(得分:1)
根据documentation,您需要在close()
之前致电join()
:
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
编辑:最好使用上下文管理器,不要忘记调用close()
:
def call_other_thing_with_multi():
with multi.Pool(3) as P:
P.map(other_thing, range(0,5))
print('END')