python-asyncio - asyncio.wait() 上的 tqdm 进度条
问题描述
试图在 asyncio.wait() 上放置一个进度条。示例代码如下。有什么办法可以使这项工作?
import asyncio
import aiohttp
from tqdm.asyncio import tqdm
async def async_url_contents(urls: list):
async with aiohttp.ClientSession() as session:
tasks = [asyncio.create_task(fetch(u, session)) for u in urls]
#tqdm simply prints task as completed in 0 seconds !!
await asyncio.wait(tqdm(tasks)) # want this to show progress bar for task wait!
return tasks
async def fetch(url: str, session:aiohttp.ClientSession):
try:
async with session.get(url) as resp:
content = await resp.text()
except IOError:
print(f"\nFailed to load {url}!!!\n")
content = None
return content
urls = ['http://cnn.com', 'http://google.com', 'http://twitter.com']
results = asyncio.get_event_loop().run_until_complete(async_url_contents(urls))
做await tqdm(asyncio.wait(tasks))
也行不通。
解决方案
推荐阅读
- hbase - 为什么 HBase 删除恢复到以前的值而不是完全删除它
- forms - 尝试创建颜色以更改 Windows::Form 的背景时出现 C2228 错误
- angular - 绑定表达式不能包含链式表达式(Angular 8)
- relayjs - 在进行节点查询时,react-relay 会在发出网络请求之前检查该节点是否在存储中吗?
- android - ScrollView 根本无法滚动
- json - 使用 JQ 将 JSON 嵌套到 CSV
- c# - DataTable.Load() 将枚举转换为整数
- python - 如何按单个单词分组并计算包含元组项的列表的平均分数?
- python - Pubsub 模拟器重新发送确认的消息
- python - Python:如何使用 IMAP 从收件箱中的某些电子邮件地址中提取姓名?