You can show the progress of asynchronous tasks by adding a done callback to each asyncio.Task object.
In this tutorial, you will discover how to show progress for asyncio tasks in Python.
Let’s get started.
Table of Contents
Need to Show Progress of Asyncio Tasks
We may issue many asynchronous tasks in an asyncio program.
These may be issued one by one using the asyncio.create_task() function or all at once using a function like asyncio.gather().
Although the asynchronous tasks may be independent, we may want to keep track of their progress.
For example, we may want to report to the user that tasks are being completed, as they are being completed.
How can we show the progress of asynchronous tasks in asyncio?
Run your loops using all CPUs, download my FREE book to learn how.
How to Show Progress
We can show progress using a done callback.
A done callback is a function that we can register on an asyncio.Task.
It is called once the task is done, either normally or if it fails.
The done callback function is a regular function, not a coroutine, and takes the asyncio.Task that it is associated with as an argument.
You can learn more about using done callback functions in the tutorial:
We can use the same callback function for all tasks and report progress in a general way, such as by reporting a message.
# callback function to show progress of tasks
# report progress of the task
We can register a callback function on each asyncio.Task that we issue.
This can be achieved using the add_done_callback() method on each task and passing it the name of the callback function.
# add a done callback to a task
Now that we know how to report progress for tasks, let’s look at a worked example.
Confused by the asyncio module API?
Download my FREE PDF cheat sheet
Example of Showing Progress of Asyncio Tasks
We can explore how to show progress for asynchronous tasks in an asyncio program.
In this example, we will define a simple coroutine task that sleeps for a random number of seconds between 0 and 10.
We will also define a done callback function to report the progress of each completed task by printing one dot on stdout.
We will then schedule 20 independent tasks and register the done callback function on each, then wait for all tasks to complete.
The complete example is listed below.
# example of showing the progress of tasks using a callback
# callback function to show the progress of tasks
# report progress of the task
# coroutine task
async def work():
# simulate effort
await asyncio.sleep(random.random() * 10)
# main coroutine
async def main():
# create and schedule many tasks
tasks = [asyncio.create_task(work()) for _ in range(20)]
# add done callback function to all tasks
for task in tasks:
# wait for all tasks to complete
_ = await asyncio.wait(tasks)
# report final message
# run the asyncio program
Running the example first creates the main() coroutine that is executed as the entry point into the asyncio program.
The main() coroutine runs and creates a list of 20 work() coroutines that are scheduled as independent tasks in a list comprehension.
The list of tasks is then traversed and the progress() done callback function is registered on each.
The main() coroutine then suspends, waiting for all tasks to complete.
Each task runs, sleeping for a random number of seconds. After each task is completed, its done callback function is called, printing a dot on stdout.
Once all tasks are completed, a final message is reported.
This highlights how a number of asynchronous tasks can be issued and their progress reported automatically using a done callback function.
Free Python Asyncio Course
Download my asyncio API cheat sheet and as a bonus you will get FREE access to my 7-day email course on asyncio.
Discover how to use the Python asyncio module including how to define, create, and run new coroutines and how to use non-blocking I/O.
This section provides additional resources that you may find helpful.
- Python Asyncio Jump-Start, Jason Brownlee, 2022 (my book).
- Python Asyncio Interview Questions
- Asyncio Module API Cheat Sheet
I also recommend the following books:
- Python Concurrency with asyncio, Matthew Fowler, 2022.
- Using Asyncio in Python, Caleb Hattingh, 2020.
- asyncio — Asynchronous I/O
- Asyncio Coroutines and Tasks
- Asyncio Streams
- Asyncio Subprocesses
- Asyncio Queues
- Asyncio Synchronization Primitives
Overwheled by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
You now know how to show progress for asyncio tasks in Python.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.