Jason Brownlee's Blog, page 9
December 5, 2023
What is asyncio.sleep(0)
You can force the current asyncio task to suspend using asyncio.sleep(0). This gives an opportunity for all other scheduled tasks in the event loop to run until their next point of suspension. This allows the event loop to progress one cycle through all tasks before resuming the current task. In this tutorial, you will discover […]
Published on December 05, 2023 10:00
December 4, 2023
Asyncio Libraries For Software Development
We can use third-party libraries to assist with common asyncio software development tasks. This includes tasks such as logging in our asyncio programs, debugging, unit testing, linting, and profiling asyncio programs. Many of these capabilities are provided in the Python standard library and can support asyncio programs, perhaps after some configuration. Nevertheless, we may be […]
Published on December 04, 2023 10:00
December 3, 2023
Log All Silent Exceptions in Asyncio
You can get the details of silent never-retrieved exceptions in asyncio once the program is terminated. A custom event loop exception handler can be defined that will be called for each never-retrieved exception, allowing these exceptions to be logged. Nevertheless, we can avoid never-retrieved exceptions by retrieving and logging them. This can be achieved manually, […]
Published on December 03, 2023 10:00
December 2, 2023
Python Asyncio Libraries: 5 Places Where To Find Them
Python asyncio development can be faster and our applications can be made more capable by using third-party libraries. The problem is finding high-quality asyncio libraries. Thankfully, there are a number of Python library repositories and curated lists that we can consult when we require a third-party asyncio library that offers a specific feature. In this […]
Published on December 02, 2023 10:00
December 1, 2023
Asyncio Dunder Methods (Magic Methods)
Asyncio brings asynchronous programming to Python. This includes a number of dunder methods (magic methods) that define behaviors expected of asynchronous objects in Python, intended to be used via specific asynchronous expressions. In this tutorial, you will discover asyncio dunder methods (magic methods) in Python. After completing this tutorial, you will know: Let’s get started. […]
Published on December 01, 2023 10:00
November 30, 2023
Find Stuck and Long Running Tasks in Asyncio
You can find all stuck long-running tasks in asyncio by manually tracking how long each task has been alive and reporting task details if a threshold “too long” time is exceeded. This approach can be used to find all stuck, hanging, and zombie asyncio tasks in the event loop, as well as those tasks that […]
Published on November 30, 2023 10:00
November 29, 2023
Python Asyncio Books
Books on asyncio remain a great way to learn asynchronous programming in Python. Asyncio is a new and exciting addition to Python3 for asynchronous programming. It can be challenging to use, especially for developers new to the asynchronous programming paradigm. Therefore, we need a good book on Python asyncio. In this article, we will review […]
Published on November 29, 2023 10:00
November 28, 2023
How to Debug Asyncio
You can debug asyncio programs by enabling debug-level logging, enabling warnings, and running the asyncio event loop in debug mode. This will report additional messages from the asyncio module in the standard library, and perform additional checks, such as calls to thread-unsafe functions and reporting when asyncio tasks take too long to complete. In this […]
Published on November 28, 2023 10:00
November 27, 2023
Asyncio Cancel All Tasks If One Task Fails
We can cancel all asyncio tasks if one task fails. It is common to group similar tasks together, then execute the group of tasks concurrently and wait for them to complete. If one task in the group fails with an unhandled exception, we can then cancel all other tasks in the group. This can be […]
Published on November 27, 2023 10:00
November 26, 2023
asyncio.timeout() To Wait and Cancel Tasks
Last Updated on November 27, 2023 We often need to execute long-running tasks in asyncio. For example, we may need to wait for a response from a remote server, for something to change, or for input from another system. It is a best practice to use a timeout when waiting for a long-running task. If […]
Published on November 26, 2023 10:00


