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.
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 loops using all CPUs, download your 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.
For example:
1 2 3 4 |
# callback function to show progress of tasks def progress(task): # report progress of the task print('.', end='') |
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.
For example:
1 2 3 |
... # add a done callback to a task task.add_done_callback(progress) |
Now that we know how to report progress for tasks, let’s look at a worked example.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# SuperFastPython.com # example of showing the progress of tasks using a callback import random import asyncio # callback function to show the progress of tasks def progress(task): # report progress of the task print('.', end='') # 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: task.add_done_callback(progress) # wait for all tasks to complete _ = await asyncio.wait(tasks) # report final message print('\nAll done.') # run the asyncio program asyncio.run(main()) |
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.
1 2 |
.................... All done. |
Free Python Asyncio Course
Download your FREE Asyncio PDF cheat sheet and get BONUS access to my free 7-day crash course on the Asyncio API.
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.
Further Reading
This section provides additional resources that you may find helpful.
Python Asyncio Books
- Python Asyncio Mastery, Jason Brownlee (my book!)
- Python Asyncio Jump-Start, Jason Brownlee.
- Python Asyncio Interview Questions, Jason Brownlee.
- 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 Recipes, Mohamed Mustapha Tahrioui, 2019.
Guides
APIs
- asyncio — Asynchronous I/O
- Asyncio Coroutines and Tasks
- Asyncio Streams
- Asyncio Subprocesses
- Asyncio Queues
- Asyncio Synchronization Primitives
References
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Takeaways
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.
Do you have any questions?