Jason Brownlee's Blog, page 8

December 14, 2023

Asyncio Timeout Best Practices

Last Updated on December 15, 2023 You can and should add timeouts to long-running tasks in asyncio programs. In this tutorial, you will discover the importance of timeouts in asyncio and how to add timeouts to your programs. Let’s get started. What is a Timeout? In asyncio, a timeout refers to a mechanism for setting […]
 •  0 comments  •  flag
Share on Twitter
Published on December 14, 2023 10:00

December 13, 2023

Asyncio WebSocket Clients

Websockets provide a full-duplex way for clients and servers to communicate on the web. It is an efficient and widely used protocol for real-time applications like chat, games, and streaming of text, audio, and/or video. The asyncio module in the Python standard library does not provide direct support for WebSockets, although we can develop WebSocket […]
 •  0 comments  •  flag
Share on Twitter
Published on December 13, 2023 10:00

December 12, 2023

Use asyncio.timeout_at() to Run Tasks With Deadlines

Last Updated on December 13, 2023 You can wait for asyncio tasks with a deadline using the asyncio.timeout_at() context manager. This asynchronous context manager will cancel the task if it takes too long and will raise an asyncio.Timeout exception, which can be handled to clean up after the task. In this tutorial, you will discover […]
 •  0 comments  •  flag
Share on Twitter
Published on December 12, 2023 10:00

December 11, 2023

Python Asyncio HTTP Client Libraries

We can use HTTP client libraries in asyncio programs. The popular Requests client HTTP library performs blocking network I/O when making requests. Using this library directly in our asyncio programs will block the event loop and prevent all other coroutines from progressing, however, there are workarounds such as running blocking calls in a separate thread. […]
 •  0 comments  •  flag
Share on Twitter
Published on December 11, 2023 10:00

December 10, 2023

Asyncio Disappearing Task Bug

Last Updated on December 11, 2023 You can have running background tasks in asyncio suddenly disappear. This is a known bug and can be avoided by ensuring that you keep a strong reference to all tasks that run in the background. In this tutorial, you will discover the asyncio disappearing task bug and how to […]
 •  0 comments  •  flag
Share on Twitter
Published on December 10, 2023 10:00

December 9, 2023

Asynchronous Requests in Python

Last Updated on December 12, 2023 We can make Async Requests in Python. The Requests Python library does not support asyncio directly. If we make HTTP requests using the Requests library, it will block the asyncio event loop and prevent all other coroutines in the program from progressing. Instead, we can make async requests using […]
 •  0 comments  •  flag
Share on Twitter
Published on December 09, 2023 10:00

Python Async Requests

We can make Async Requests in Python. The Requests Python library does not support asyncio directly. If we make HTTP requests using the Requests library, it will block the asyncio event loop and prevent all other coroutines in the program from progressing. Instead, we can make async requests using the asyncio.to_thread() method provided in the […]
 •  0 comments  •  flag
Share on Twitter
Published on December 09, 2023 10:00

December 8, 2023

Python Async Web Servers and Frameworks

Asyncio has found a home in Python web development. Nevertheless, the landscape of async web development is changing fast. It’s also confusing because there are very old projects that are described as “asynchronous” and very modern projects that are async-first, leaving the latest fast event loops and language features. Let’s slow things down a bit […]
 •  0 comments  •  flag
Share on Twitter
Published on December 08, 2023 10:00

December 7, 2023

Asyncio Context Variables For Shared State

Last Updated on December 8, 2023 You can have task local storage in asyncio programs using context variables in the contextvars module. This provides thread-local-like storage for arbitrary contexts within one thread, such as within tasks in an asyncio program. In this tutorial, you will discover how to use context variables for private and local […]
 •  0 comments  •  flag
Share on Twitter
Published on December 07, 2023 10:00

December 6, 2023

Python Asyncio Alternatives

We can use asyncio for asynchronous programming in Python, but we don’t have to. There are alternatives to asyncio. Some are old, widely used, and trusted, and others are new, interesting, and potentially a lot faster. Asyncio is here to stay, but we can learn more about how it fits into the ecosystem by considering […]
 •  0 comments  •  flag
Share on Twitter
Published on December 06, 2023 10:00