Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Jason Brownlee's Blog

May 14, 2024

Asyncio gather() Return Values

We can retrieve return values from coroutines executed concurrently by asyncio.gather(). The asyncio.gather() function returns an asyncio.Future that executes all provided coroutines concurrently. Once the asyncio.Future is done, it returns a list that contains one return value for each coroutine provided to asyncio.gather(), in the order that they were provided. In this tutorial, you will […]
 •  0 comments  •  flag
Share on Twitter
Published on May 14, 2024 12:00

May 12, 2024

Asyncio gather() In The Background

We can run asyncio.gather() in the background by not awaiting the call to asyncio.gather(). The asyncio.gather() returns an asyncio.Future that does not have to be awaited. It is scheduled for execution in the asyncio event loop, along with all coroutines provided to the gather. Therefore, the caller is free to proceed with other activities and […]
 •  0 comments  •  flag
Share on Twitter
Published on May 12, 2024 12:00

May 9, 2024

Asyncio gather() Limit Concurrency

We can limit concurrency when using asyncio.gather() via a semaphore. In this tutorial, you will discover how to limit concurrency with asyncio.gather(). Let’s get started. Need to Limit Concurrency with asyncio.gather() The asyncio.gather() function allows us to run multiple coroutines or tasks concurrently. Coroutines can be provided as positional arguments to asyncio.gather() and a list […]
 •  0 comments  •  flag
Share on Twitter
Published on May 09, 2024 12:00

May 7, 2024

Asyncio gather() Exception in Task Does Not Cancel

We can execute coroutines concurrently with asyncio.gather(). By default, an exception in asyncio.gather() will be propagated to the caller immediately. The asyncio.gather() call will not wait for the other coroutines to complete, and it will not cancel the other tasks. It is critically important to understand the default behavior of asyncio.gather() because it is the […]
 •  0 comments  •  flag
Share on Twitter
Published on May 07, 2024 12:00

May 5, 2024

Asyncio gather() Timeout

We can add a timeout when using asyncio.gather() via the asyncio.timeout() context manager. A timeout in seconds can be specified to the asyncio.timeout() context manager and the asyncio.gather() function can be called within the context manager block. If the timeout elapses before all tasks in the gather() are done, an asyncio.TimeoutError exception is raised. Using […]
 •  0 comments  •  flag
Share on Twitter
Published on May 05, 2024 12:00

May 2, 2024

Asyncio Shield Main Coroutine From Cancellation

We can simulate shielding the main coroutine from cancellation by using a wrapper coroutine that consumes cancellation requests. In this tutorial, you will discover how to shield the main coroutine from cancellation. Let’s get started. Need to Shield The Main Coroutine From Cancellation Asyncio tasks can be canceled at any time. The main coroutine is […]
 •  0 comments  •  flag
Share on Twitter
Published on May 02, 2024 12:00

April 30, 2024

Asyncio Run Multiple Concurrent Event Loops

We can run multiple concurrent asyncio event loops by starting and running each new event loop in a separate thread. Each thread can host and manage one event loop. This means we can start one thread per event loop we require, allowing a program to potentially scale from thousands to millions of coroutines. In this […]
 •  0 comments  •  flag
Share on Twitter
Published on April 30, 2024 12:00

April 28, 2024

Asyncio gather() TypeError: unhashable type: ‘list’

We can avoid a TypeError exception when using asyncio.gather() by unpacking the collection of awaitables with the star (*) operator. The asyncio.gather() function is used to execute multiple coroutines concurrently and return a list of return values once all tasks are done. The function will fail with a TypeError exception if a list or set […]
 •  0 comments  •  flag
Share on Twitter
Published on April 28, 2024 12:00

April 25, 2024

Asyncio Event Loop in Separate Thread

We can run an asyncio event loop in a new thread by starting a new thread and configuring it to start or run an event loop. There are many approaches we can use to run an event loop in a new thread. The simplest is to configure a new thread to start an event loop […]
 •  0 comments  •  flag
Share on Twitter
Published on April 25, 2024 12:00

April 23, 2024

Asyncio gather() Handle Exceptions

We can automatically handle exceptions in coroutines executed via asyncio.gather() by setting the “return_exceptions” argument to True. By default, if a coroutine is executed by asyncio.gather() fails with an unhandled exception, it will be propagated to the caller. Setting the “return_exceptions” argument to True will trap any unhandled exceptions and provide them as return values, […]
 •  0 comments  •  flag
Share on Twitter
Published on April 23, 2024 12:00