首页 > 解决方案 > 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))也行不通。

标签: python-asynciotqdm

解决方案


推荐阅读