dur*_*2.0 5 python multiprocessing python-3.x python-multiprocessing
我正在尝试将某些代码从Python 3.6移植到Windows 10上的Python 3.7。我看到在调用.get()该AsyncResult对象时多处理代码挂起。有问题的代码要复杂得多,但是我将其简化为类似于以下程序的代码。
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)
Run Code Online (Sandbox Code Playgroud)
此代码也可以在Python 2.7中运行。出于某种原因,第一个调用get()在3.7中挂起,但是在其他版本上,一切正常。
我觉得这是描述在Python 3.7.2回归这里。它似乎只在 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))
Run Code Online (Sandbox Code Playgroud)
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.
| 归档时间: |
|
| 查看次数: |
389 次 |
| 最近记录: |