python 协程 asyncio_python – asyncio.as_completed是否会产生期货或协同程序?
发布日期:2021-06-24 13:24:21 浏览次数:3 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java设定xml文件的encoding_配置web-xml解决中文乱码问题,及各种乱码问题集结
下一篇:Java虚拟机不能满足_深入理解Java虚拟机--读书笔记1/3

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月19日 00时14分49秒