問題描述
我正在嘗試將一些代碼從 Python 3.6 移植到 Windows 10 上的 Python 3.7.在 AsyncResult
上調(diào)用 .get()
時(shí),我看到多處理代碼掛起目的.有問題的代碼要復(fù)雜得多,但我已將其歸結(jié)為類似于以下程序的代碼.
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 中運(yùn)行.出于某種原因,對(duì) get()
的第一次調(diào)用在 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.
推薦答案
我認(rèn)為這是 Python 3.7.2 中描述的回歸 這里.它似乎只在 virtualenv 中運(yùn)行時(shí)影響用戶.
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.
暫時(shí)您可以通過執(zhí)行在此錯(cuò)誤線程的評(píng)論中描述的操作來解決它.
import _winapi
import multiprocessing.spawn
multiprocessing.spawn.set_executable(_winapi.GetModuleFileName(0))
這將強(qiáng)制子進(jìn)程使用 real python.exe 而不是 virtualenv 中的那個(gè)生成.因此,如果您使用 PyInstaller 將內(nèi)容捆綁到 exe 中,這可能不合適,但在使用本地 Python 安裝從 CLI 運(yùn)行時(shí),它可以正常工作.
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.
這篇關(guān)于多處理 AsyncResult.get() 在 Python 3.7.2 中掛起,但在 3.6 中沒有的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!