多处理AsyncResult.get()在Python 3.7.2中挂起,但在3.6中挂起 [英] Multiprocessing AsyncResult.get() hangs in Python 3.7.2 but not in 3.6

查看:172
本文介绍了多处理AsyncResult.get()在Python 3.7.2中挂起,但在3.6中挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows 10上将一些代码从Python 3.6移植到Python 3.7.我看到在AsyncResult对象上调用.get()时,多处理代码挂起.有问题的代码要复杂得多,但我将其简化为类似于以下程序的代码.

I'm trying to port some code from Python 3.6 to Python 3.7 on Windows 10. I see the multiprocessing code hang when calling .get() on the AsyncResult object. The code in question is much more complicated, but I've boiled it down to something similar to the following program.

import multiprocessing


def main(num_jobs):
    num_processes = max(multiprocessing.cpu_count() - 1, 1)
    pool = multiprocessing.Pool(num_processes)

    func_args = []
    results = []

    try:
        for num in range(num_jobs):
            args = (1, 2, 3)
            func_args.append(args)
            results.append(pool.apply_async(print, args))

        for result, args in zip(results, func_args):
            print('waiting on', args)
            result.get()
    finally:
        pool.terminate()
        pool.join()


if __name__ == '__main__':
    main(5)

此代码也可以在Python 2.7中运行.由于某些原因,对get()的首次调用在3.7中挂起,但是在其他版本上一切正常.

This code also runs in Python 2.7. For some reason the first call to get() hangs in 3.7, but everything works as expected on other versions.

推荐答案

我认为这是Python 3.7.2中的回归,如

I think this is a regression in Python 3.7.2 as described here. It seems to only affect users when running in a virtualenv.

暂时,您可以通过在此错误线程注释中进行中所述的方法来解决此问题.

For the time being you can work-around it by doing what's described in this comment on the bug thread.

import _winapi
import multiprocessing.spawn
multiprocessing.spawn.set_executable(_winapi.GetModuleFileName(0))

这将迫使子进程使用 real python.exe而不是virtualenv中的子进程生成.因此,如果您使用PyInstaller将内容捆绑到exe中,则可能不合适,但是当使用本地Python安装从CLI运行时,它可以正常工作.

That will force the subprocesses to spawn using the real python.exe instead of the one that's in the virtualenv. So, this may not be suitable if you're bundling things into an exe with PyInstaller, but it works OK when running from the CLI with local Python installation.

这篇关于多处理AsyncResult.get()在Python 3.7.2中挂起,但在3.6中挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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