import types
from typing import Any, Awaitable
@types.coroutine
def _async_yield(obj):
return (yield obj)
async def gather(*aws: Awaitable[Any], return_exceptions: bool = False):
result_array: list[Any] = [None for _ in range(len(aws))]
done, pending = [], [*aws]
while pending:
for i, awaitable in enumerate(aws):
if awaitable in done:
continue
try:
next(awaitable.__await__())
await _async_yield(None)
except StopIteration as exc:
result_array[i] = exc.value
done.append(awaitable)
pending.remove(awaitable)
except BaseException as exc:
if not return_exceptions:
raise
result_array[i] = exc
done.append(awaitable)
pending.remove(awaitable)
return result_array
async def test_wait():
async def a():
return 1
async def b():
import asyncio
await asyncio.sleep(0)
return 2
async def c():
import asyncio
await asyncio.sleep(1)
return 3
return await gather(a(), b(), c(), asyncio.create_task(c()))
if __name__ == "__main__":
import asyncio
print(asyncio.run(test_wait()))
报错所处的位置:
# asyncio/futures.py line 281
def __await__(self):
if not self.done():
self._asyncio_future_blocking = True
yield self # This tells Task to wait for completion.
if not self.done():
raise RuntimeError("await wasn't used with future")
return self.result() # May raise too.
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.