Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 多处理池挂起在jupyter笔记本中_Python 2.7_Multiprocessing - Fatal编程技术网

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