Python:多处理imap&动态创建的池冻结 [英] Python: Multiprocessing imap & dynamically-created pools freeze

查看:50
本文介绍了Python:多处理imap&动态创建的池冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么此代码冻结(永久挂起)

Could someone explain why this code freezes (hangs forever),

import multiprocessing
def foo(x): return x
def main():
    print(list(multiprocessing.Pool().imap(foo, iter([1, 2, 3]))))
main()

虽然此变体可以正常工作,

While this variant works fine,

import multiprocessing
def foo(x): return x
pool = multiprocessing.Pool()
def main():
    print(list(pool.imap(foo, iter([1, 2, 3]))))
main()

与此一样,

import multiprocessing
def foo(x): return x
def main():
    print(list(multiprocessing.Pool().map(foo, [1, 2, 3])))
main()

顺便说一句,能够在应用程序的非初始化阶段动态创建multiprocessing.Pool(),然后使用imap确实会有所帮助.我尝试在gdb中逐步进行调试,但似乎在PyThread_acquire_lock中冻结了……但是在这样低的水平下,我感觉调试很不合理.

Incidentally, being able to dynamically create multiprocessing.Pool() at a non-initialization stage in the application, and then use imap would be really helpful. I tried stepping through it in gdb, and it seems frozen at PyThread_acquire_lock ... but I feel pretty out-of-element debugging at such a low level.

推荐答案

显然,这是Python 2.6.7的错误,并且至少已由Python 2.7.3修复.

Apparently this is a bug with Python 2.6.7, and has been fixed at least by Python 2.7.3.

这可以通过使用我的vimap包[ https://github.com/gatoatigrado/vimap来解决. ].

This can be worked around by using my vimap package [ https://github.com/gatoatigrado/vimap ].

这篇关于Python:多处理imap&动态创建的池冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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