Jason Brownlee's Blog, page 5

February 11, 2024

Asyncio Log Task Exceptions With Done Callback Function

Last Updated on February 12, 2024 You can log task exceptions automatically in asyncio by defining a general done callback that checks the task for an exception, and if present, logs it. This general done callback function can then be added routinely to all tasks created in the program to ensure that task exceptions are […]
 •  0 comments  •  flag
Share on Twitter
Published on February 11, 2024 10:00

February 8, 2024

Asyncio Task Exception Was Never Retrieved

Your asyncio program may report a warning: Task exception was never retrieved. Asyncio will report this message when the program is terminated and there were tasks that failed with an exception and the exception was never retrieved. It is a good practice to retrieve task exceptions if possible, otherwise to log all never retrieved exceptions […]
 •  0 comments  •  flag
Share on Twitter
Published on February 08, 2024 10:00

February 6, 2024

Async For Loop in Python

You can develop an asynchronous for-loop in asyncio so all tasks run concurrently. There are many ways to develop an async for-loop, such as using asyncio.gather(), use asyncio.wait(), use asyncio.as_completed(), create and await a list of asyncio.Task objects and use an asyncio.TaskGroup. In this tutorial, you will discover how to execute an asyncio for loop […]
 •  0 comments  •  flag
Share on Twitter
Published on February 06, 2024 10:00

February 4, 2024

Asyncio Logging Best Practices

Asyncio programs should log, like any other production-quality Python program. There are special considerations and best practices when logging from asyncio programs. These practices have been collected together into this asyncio logging best practices guide. In this tutorial, you will discover the best practices for logging in asyncio programs in Python. Let’s get started. What […]
 •  0 comments  •  flag
Share on Twitter
Published on February 04, 2024 10:00

February 1, 2024

What is the Main Coroutine

The coroutine provided to asyncio.run() to start the asyncio event loop is called the main coroutine or the main task. It has special properties, such as when it is done, the asyncio event loop is shut down and all other tasks are canceled. In this tutorial, you will discover the main coroutine in asyncio programs. […]
 •  0 comments  •  flag
Share on Twitter
Published on February 01, 2024 10:00

January 30, 2024

How to Kill All Asyncio Tasks

You cannot immediately terminate the asyncio event loop, or kill all running tasks. Instead, we must enumerate all running tasks and request that each be canceled. This will cause all tasks to raise a CancelledError exception and unwind and match the way the asyncio event loop will automatically cancel all tasks when it is exited […]
 •  0 comments  •  flag
Share on Twitter
Published on January 30, 2024 10:00

January 28, 2024

Asyncio Non-Blocking Logging With aiologger

You can log without blocking in asyncio programs by using aiologger. In this tutorial, you will discover how to log in asyncio programs without blocking using the aiologger library. Let’s get started. Logging From Asyncio is Blocking Logging from an asyncio program is blocking. This means that logging a is a function call that will […]
 •  0 comments  •  flag
Share on Twitter
Published on January 28, 2024 10:00

January 25, 2024

Asyncio Log Long-Running Blocking Calls With aiodebug

You can identify and log asyncio tasks that block the event loop for too long. Calling Python functions in asyncio programs rather than awaiting coroutines and tasks will block the event loop. Function calls that take a long time, such as those that perform file I/O or a CPU-bound task may cause the event loop […]
 •  0 comments  •  flag
Share on Twitter
Published on January 25, 2024 10:00

January 23, 2024

Asyncio CancelledError Can Be Consumed

Asyncio tasks can suppress requests to be canceled and actively consume CancelledError exceptions. Although consuming CancelledError exceptions is discouraged, it is possible to develop tasks that do this. Nevertheless, there are cases where we may think that a CancelledError exception will be consumed, when in fact it is not and is allowed to propagate normally. […]
 •  0 comments  •  flag
Share on Twitter
Published on January 23, 2024 10:00

January 21, 2024

4 Ways to Suppress Asyncio CancelledError

You can suppress CancelledError exceptions in asyncio by handling them within the task or in the caller that canceled the task. Alternatively, they can be suppressed in one line by awaiting asyncio.gather() and configuring it to return exceptions, or via the contextlib.suppress() context manager. In this tutorial, you will discover how to suppress the CancelledError […]
 •  0 comments  •  flag
Share on Twitter
Published on January 21, 2024 10:00