本文共 2060 字,大约阅读时间需要 6 分钟。
来自asyncio docs:
asyncio.as_completed(aws,*,loop=None,timeout=None)
Run awaitable objects in the aws set concurrently. Return an iterator
of Future objects. Each Future object returned represents the earliest
result from the set of the remaining awaitables.
我假设这些Future对象中的每一个都具有asyncio.Future中描述的方法:.cancelled(),. exception()和.result().但似乎所产生的元素只是协程,而不是Future对象.我错过了什么?
这似乎打败了.as_completed()的描述.如果我需要等待它,协程如何“完成”?
>>> import asyncio
>>> import aiohttp
>>>
>>> async def get(session,url):
... async with session.request('GET',url) as resp:
... t = await resp.text()
... return t
...
>>> async def bulk_as_completed(urls):
... async with aiohttp.ClientSession() as session:
... aws = [get(session,url) for url in urls]
... for future in asyncio.as_completed(aws):
... for i in ('cancelled','exception','result'):
... print(hasattr(future,i))
... print(type(future))
... try:
... result = await future
... except:
... pass
... else:
... print(type(result))
... print()
...
>>>
>>> urls = (
... 'https://docs.python.org/3/library/asyncio-task.html',... 'https://docs.python.org/3/library/select.html',... 'https://docs.python.org/3/library/this-page-will-404.html',... )
>>>
>>> asyncio.run(bulk_as_completed(urls))
False
False
False
最终,我关心这个的原因是因为我想让异常像asyncio.gather(…,return_exceptions = True)那样冒泡.考虑在调用session.request()时添加一个伪造的URL:
urls = (
'https://docs.python.org/3/library/asyncio-task.html','https://docs.python.org/3/library/select.html','https://docs.python.org/3/library/this-page-will-404.html',# This URL will raise on session.request(). How can I propagate
# that exception to the iterator of results?
'https://asdfasdfasdf-does-not-exist-asdfasdfasdf.com'
)
我希望能够做的是这样的事情(使用Future对象的方法,但这些都不是Future对象,这就是问题):
async def bulk_as_completed(urls):
async with aiohttp.ClientSession() as session:
aws = [get(session,url) for url in urls]
for future in asyncio.as_completed(aws):
if future.cancelled():
res = futures.CancelledError()
else:
exc = future.exception()
if exc is not None:
res = exc
else:
res = future.result()
# ...
# [Do something with `res`]
转载地址:https://blog.csdn.net/weixin_33137081/article/details/114913597 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!