【发布时间】:2021-01-27 00:38:33
【问题描述】:
假设您希望以异步方式在循环中启动多个并行进程,当然,为每个进程获取其退出代码并打印。
如果我们做一些天真的事情:
i = 5
While i > 0:
p = sp.Popen(["./my_module.py", "arg1", "arg2"], stdout=sp.PIPE, stderr=sp.PIPE)
out, err = p.communicate()
result = p.returncode
i -= 1
由于上面的 communicate 阻塞方法,这可能无法异步工作。
对于实现多进程分配异步方式并最终将结果累积在列表或其他东西中的最佳简单方法有什么想法吗?
【问题讨论】:
-
这能回答你的问题吗? Non blocking subprocess.call
-
@ThiagoBarcala:我还没有看到任何具体的解决方案来累积多个进程运行的结果,此外我更喜欢避免异步
标签: python python-3.x multiprocessing subprocess