我正在尝试使用 asyncio.create_subprocess_exec 启动一堆子进程:procs = []for i in range(5): proc = await asyncio.create_subprocess_exec(...) procs.append(proc.wait()) # doesn't work with this line either: procs.append(asyncio.create_task(proc.wait()))await asyncio.wait(*procs,..)# doesn't work with await asyncio.as_completed either. asyncio.gather do work, but I'd like to add timeout limit and return back to my code as earlier as possible.例外是:TypeError: expect a list of futures, not Task against line 'await asyncio.wait(*procs,..) Python 的版本是 3.8。尽管在官方文档中警告不要自己创建未来,但如果有办法将proc转换为 Future,我想尝试一下。
1 回答
喵喔喔
TA贡献1735条经验 获得超5个赞
正如消息所说,您需要提供一份期货清单。使用*运算符,您可以解包列表并将其成员作为单独的位置参数传递。正确的调用是:
await asyncio.wait(procs)
您的原始代码之所以有效,gather()是因为gather()实际上确实期望将可等待对象作为单独的位置参数。
添加回答
举报
0/150
提交
取消
