Jason Brownlee's Blog, page 2
April 21, 2024
Asyncio Event Loop Exception Handler
We can configure a custom asyncio event loop exception handler via the asyncio.get_running_loop() method. By default, unhandled exceptions in asyncio programs cause the event loop to emit a warning and are reported using a default exception handler once the event loop is closed. Setting a custom exception handler allows our programs to intentionally handle never-retrieved […]
Published on April 21, 2024 12:00
April 18, 2024
Asyncio Coroutine Object Methods in Python
We can define coroutine methods on custom Python objects. This allows methods on custom Python objects to use async/await syntax, such as awaiting other coroutines and tasks and allows the custom coroutine methods themselves to be awaited within our asyncio programs. In this tutorial, you will discover how to define object methods as coroutines. Let’s […]
Published on April 18, 2024 12:00
April 16, 2024
Asyncio Handle Control-C (SIGINT)
The “Signal Interrupt” or SIGINT signal is raised in a program when the user presses Ctrl-C. This has the effect of interrupting and often terminating a Python program. The SIGINT signal can be used to terminate asyncio programs as well, and we can use a custom signal handler function to perform cleanup activities before the […]
Published on April 16, 2024 12:00
April 14, 2024
How to Create Task in Done Callback
We can schedule asyncio tasks in the event loop from done callback functions. Done callback functions are regular Python functions executed after a task is done. Because they are regular Python functions, they cannot await tasks or coroutines. Nevertheless, we can create and schedule new tasks from done callback functions, and this may require using […]
Published on April 14, 2024 12:00
April 11, 2024
Asyncio Concurrent Tasks
We can execute asyncio tasks and coroutines concurrently, a main benefit of using asyncio. There are four main ways that we can achieve this, including issuing coroutines as independent tasks and awaiting them directly, awaiting them automatically via a TaskGroup, using asyncio.wait() or using asyncio.gather(). In this tutorial, you will discover how to execute asyncio […]
Published on April 11, 2024 12:00
April 9, 2024
Asyncio Suspend Forever
The asyncio.Server in the asyncio module provides a way to suspend the main coroutine forever and accept client connections. Reviewing the code in the standard library, we can see that this is achieved by creating a new and empty asyncio.Future and await it. We can use this approach in our own asyncio server applications to […]
Published on April 09, 2024 12:00
April 7, 2024
Asyncio Custom Awaitable With __await__()
We can define a custom awaitable for use in asyncio programs. This can be achieved by defining a Python object that implements the __await__() method that either yields execution or delegates or returns the result of another awaitables __await__() method. In this tutorial, you will discover how to define Python objects with the __await__() method. […]
Published on April 07, 2024 12:00
April 4, 2024
Asyncio Coroutine Chaining
We can chain coroutines together into linear sequences In other languages, this is called promise chaining or future chaining. This allows a pipeline of dependent or independent tasks to be completed in a prescribed order. There are many ways we can achieve this in asyncio programs, including having coroutines manually chain themselves together, using a […]
Published on April 04, 2024 11:00
April 2, 2024
Asyncio Run Event Loop Forever
We can run an asyncio event loop “forever”, until explicitly stopped or killed. This is required in many applications that offer a service, accept client connections, or perform long-term event handling. In this tutorial, you will discover how to run an asyncio program forever. After completing this tutorial, you will know: Let’s get started. Need […]
Published on April 02, 2024 11:00
March 31, 2024
Asyncio Echo Unix Socket Server
We can develop an echo client and server with Unix sockets in asyncio. An echo client and server is a simple protocol where the server expects to receive one message, and then sends it back to the client. Unix sockets are a simple and efficient way of connecting processes using a file-based socket. As their […]
Published on March 31, 2024 11:00


