tqdm for asyncio in jupyter notebook

Viewed 274

I'm using tqdm for an async action in my Jupyter notebook -- should I use tqdm.notebook (which I think gets me the right widget) or tqdm.asyncio (which probably gets me the right behavior -- the items/sec calc seems off in my use).

1 Answers

You can use an asyncio compatible version of tqdm in a jupyter notebook, by using the solution from this stackoverflow thread:

and replacing the tqdm you use, with the notebook version:

from tqdm.notebook import tqdm

async def factorial(name, number):
    f = 1
    for i in range(2, number + 1):
        await asyncio.sleep(random.random())
        f *= i
    return f"Task {name}: factorial {number} = {f}"

requests = [factorial("A", 2), factorial("B", 3), factorial("C", 4)]
responses = [await f for f in tqdm(asyncio.as_completed(requests), total=len(requests))]
Related