Python Byte-Sized: Async Timeouts & Server-Sent Events

·36m 01s
Shared point

Async IO Management

Async Timeout Context Managers

• The hosts discuss Async Timeout, a library that provides an asyncio-compatible timeout class.
• It functions as a context manager, automatically cancelling inner async work if it exceeds the specified duration, raising a TimeoutError.
• This approach is often more efficient than asyncio.wait_for, as it avoids creating separate tasks.

Custom Context Managers in Python

Trey Hunter’s article on creating custom context managers is highlighted as a great resource for understanding __enter__ and __exit__ dunder methods.
• The discussion covers practical use cases like environment variable management and transaction handling in SQLAlchemy.
• Mention of contextlib is made as a convenient way to implement these using decorators and yield.

Web Communication

Server-Sent Events (SSE) with HTTPX

• SSE provides a lightweight, one-way communication channel from server to client, ideal for dashboards.
• The library httpx-sse is recommended for consuming these events in Python, offering a clean async API.
• The hosts demonstrate how easy it is to handle event streams with minimal boilerplate code.

Retries and Reliability

• Integration with the library stamina allows for robust, asynchronous retries when connections are interrupted.

Modern Development Extras

• A useful PyPI tip was shared: using project_urls in pyproject.toml to customize link icons in your repository metadata.
• News touched on recent successful fusion energy breakthroughs, noting the significant Python involvement in the project.
• The show concluded with a humorous look at how AI auto-complete tools can sometimes generate unexpectedly cynical code commentary.

"If you ever find yourself writing try...finally, and the finally part is unwinding something... that's a super good chance to be using a context manager."

Topics

Chapters

6 chapters
Python Bytes
AI chat — answers grounded in episodes