gpt4 book ai didi

python - 多处理 AsyncResult.get() 在 Python 3.7.2 中挂起,但在 3.6 中不挂起

转载 作者:太空狗 更新时间:2023-10-30 01:18:10 25 4
gpt4 key购买 nike

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

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 中挂起,但在其他版本上一切正常。

最佳答案

我认为这是 Python 3.7.2 中的回归,如 here 所述.它似乎只影响在 virtualenv 中运行时的用户。

目前您可以通过执行描述的操作来解决它 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 文件中,这可能不适合,但是当从 CLI 运行本地 Python 安装时它可以正常工作。

关于python - 多处理 AsyncResult.get() 在 Python 3.7.2 中挂起,但在 3.6 中不挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com