How to Show Progress of Asyncio Tasks
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?
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:
# 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:
...
# 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.
# 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.
....................
All done.
Takeaways
You now know how to show progress for asyncio tasks in Python.
If you enjoyed this tutorial, you will love my book: Python Asyncio Jump-Start. It covers everything you need to master the topic with hands-on examples and clear explanations.