[英]asyncio.gather throws RuntimeError: Task got bad yield
I want to run a few tasks "in parallel" using asyncio.gather() :我想使用asyncio.gather() “并行”运行一些任务:
import asyncio
async def foo():
return 42
async def main():
results = await asyncio.gather(foo() for i in range(10))
print(results)
asyncio.run(main())
But it fails with RuntimeError: Task got bad yield
:但它失败了RuntimeError: Task got bad yield
:
/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/events.py:88: RuntimeWarning: coroutine 'foo' was never awaited
self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 583, in run_until_complete
return future.result()
File "<stdin>", line 2, in main
File "<stdin>", line 2, in <genexpr>
RuntimeError: Task got bad yield: <coroutine object foo at 0x10555bd40>
The function asyncio.gather()
accepts each task as separate argument.函数asyncio.gather()
接受每个任务作为单独的参数。 Like this:像这样:
await asyncio.gather(task1, task2, task3)
So the solution is to replace所以解决办法是更换
results = await asyncio.gather(foo() for i in range(10))
with和
results = await asyncio.gather(*[foo() for i in range(10)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.