tqdm progress bar on asyncio.wait()

Viewed 518

Trying to put a progress bar on asyncio.wait(). Sample code is given below. Is there any way to make this work?

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))

Doing await tqdm(asyncio.wait(tasks)) also doesn't work.

0 Answers
Related