In my earlier blog post, we have discussed about how to use the progress bars tool tqdm for showing the progress of multiprocessing tasks. Since Python asyncio is an another way to run things concurrently, in addition to Python multiprocessing, it is also natural to extend the usage of tqdm to showing the progress of single-threaded asyncio asynchronous tasks.
In this blog post, I would like to briefly discuss how to use tqdm for asyncio concurrent tasks.
As the simplest example, We will run multiple sleep asynchronously and show the progress bar using tqdm.
We could use aiohttp to download asynchronously. Here I have an example for downloading PDBx files from RCSB PDB Bank asynchronously using aiohttp and tqdm.
The tqdm usages were almost the same as the async sleep example above.
tqdm shows that the progress bar could also be applied for async for.
However, there is a common misunderstanding that async for is running for loop concurrently. Actually async for is just the counter part of the synchronous for in asyncio, with the ability for the loop in question to give up control to the event loop for some other coroutine to run.
This means that async for will not run things concurrently and accelerate your application. For example, none of the following programs are running sleep concurrently.