Jason Brownlee's Blog, page 4

March 5, 2024

InvalidStateError: Exception is not set

You can get an InvalidStateError exception when attempting to retrieve an exception from an asyncio Task. This will happen if we attempt to retrieve an exception from a task while the task is still running, e.g. the task is not yet done. We can avoid the InvalidStateError exception by waiting for the task to be […]
 •  0 comments  •  flag
Share on Twitter
Published on March 05, 2024 10:00

March 3, 2024

InvalidStateError: Result is not set

You can get an InvalidStateError exception when attempting to retrieve a return value result from an asyncio Task. This will happen if we retrieve a result via the result() method on a task while the task is still running, e.g. the task is not yet done. We can avoid the InvalidStateError exception by waiting for […]
 •  0 comments  •  flag
Share on Twitter
Published on March 03, 2024 10:00

February 29, 2024

SyntaxError: ‘await’ outside function

You can fix a “SyntaxError ‘await’ outside function” by ensuring that all await expressions are within coroutines. We can get a SyntaxError if we try to await a coroutine or asyncio.Task outside of a coroutine, such as within a function, method, or lambda. When developing asyncio programs, we need to ensure that all of our […]
 •  0 comments  •  flag
Share on Twitter
Published on February 29, 2024 10:00

February 27, 2024

Asyncio Periodic Task

You can run an asyncio task periodically in the background. This requires developing a new periodic() coroutine that runs in a loop forever, each iteration sleeping for a given number of seconds and awaiting a target coroutine. In this tutorial, you will discover how to develop a periodic task in asyncio programs. Let’s get started. […]
 •  0 comments  •  flag
Share on Twitter
Published on February 27, 2024 10:00

February 25, 2024

Asyncio Barrier in Python

An asyncio barrier is a synchronization primitive, like a sempahore and a mutex lock. It is used to coordinate the behavior of concurrent tasks at one point, on the barrier itself. Once all expected parties arrive at the barrier, the barrier is lifted and all waiting tasks can resume their activity. In this tutorial, you […]
 •  0 comments  •  flag
Share on Twitter
Published on February 25, 2024 10:00

February 22, 2024

Async Lambda in Python

You cannot define an async lambda in Python at the time of writing, at least not via the “async lambda” syntax. Nevertheless, there are a number of workarounds we can use to simulate an asynchronous lambda in Python, including defining an anonymous coroutine via an async generator. In this tutorial, you will discover asynchronous lambdas […]
 •  0 comments  •  flag
Share on Twitter
Published on February 22, 2024 10:00

February 20, 2024

Async Function in Python

You can define and run async functions in Python. An async function in Python is called a coroutine and can be defined via the “async def” expression. Unlike a regular function, it can choose when to suspend its execution using the “await” expression. In this tutorial, you will discover async functions in Python. Let’s get […]
 •  0 comments  •  flag
Share on Twitter
Published on February 20, 2024 10:00

February 18, 2024

Run One-Off Coroutine Outside of Asyncio

A coroutine cannot be run directly outside of a Python program. Attempting to “call” a coroutine will result in RuntimeWarning messages. Attempting to await a coroutine from a Python program results in a SyntaxError error. In this tutorial, you will discover how to run a one-off coroutine outside an asyncio program. Let’s get started. Need […]
 •  0 comments  •  flag
Share on Twitter
Published on February 18, 2024 10:00

February 15, 2024

Asyncio Module Logging

You can enable logging in the asyncio module in order to record additional info, warning, and debug messages. The asyncio module will log messages automatically which is helpful if we are using the logging module in our application, as the log messages will be captured along with our application log messages. The asyncio module logging […]
 •  0 comments  •  flag
Share on Twitter
Published on February 15, 2024 10:00

February 13, 2024

Asyncio Log CancelledError Exceptions

You can log CancelledError exceptions within the task that is canceled or in the caller that requested that the task be canceled. Logging the CancelledError exception provides help when debugging an asyncio program later, confirming that tasks were canceled. By logging the CancelledError exception at the point of cancellation, the log will also include details […]
 •  0 comments  •  flag
Share on Twitter
Published on February 13, 2024 10:00