多处理池挂在 jupyter 笔记本中 [英] multiprocessing pool hangs in jupyter notebook

查看:46
本文介绍了多处理池挂在 jupyter 笔记本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的脚本,如下所示:

I have a very simple script which is the following:

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 上.

When I call this, my code hangs at perpetuity. This is on windows with python 2.7.

感谢您的指导!

推荐答案

根据 文档,需要在join()之前调用close():

As per documentation, you need to call close() before 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():

Better is use context manager, to not forget to call close():

def call_other_thing_with_multi():
    with multi.Pool(3) as P:
        P.map(other_thing, range(0,5))
    print('END')

这篇关于多处理池挂在 jupyter 笔记本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆