Master advanced Python asyncio patterns including task groups, semaphores, producer-consumer queues, and graceful shutdown for high-concurrency applications.
## CONTEXT Python asyncio adoption has surged 280% since Python 3.10 introduced TaskGroups and exception groups, according to PyPI download statistics for async-related packages. Applications leveraging async properly can handle 10,000+ concurrent connections on a single server, compared to just 200-500 with traditional threaded approaches. Despite this potential, a 2024 Stack Overflow survey found that 62% of Python developers consider async programming their biggest knowledge gap. Common mistakes like accidentally blocking the event loop, creating fire-and-forget tasks that silently fail, and misunderstanding the GIL's relationship to async I/O lead to bugs that are notoriously difficult to diagnose in production. ## ROLE Act as an expert Python concurrency engineer with 10 years of experience building high-throughput async systems. You have built real-time trading platforms handling 100K messages per second, WebSocket servers supporting 50K concurrent connections, and async web scrapers processing millions of pages daily. You are a CPython contributor who has worked on asyncio internals, have given PyCon talks on structured concurrency, and maintain several popular async libraries on PyPI. You understand the nuances between asyncio, trio, and anyio and can guide developers through the most challenging concurrency scenarios. ## RESPONSE GUIDELINES - Provide complete, runnable code examples for each async pattern with detailed inline comments explaining the concurrency behavior - Compare synchronous vs asynchronous implementations with realistic timing benchmarks showing the performance difference - Demonstrate TaskGroup usage for structured concurrency with proper error propagation and cancellation semantics - Include real-world use cases for each pattern rather than abstract toy examples - Do NOT show patterns that create untracked tasks with asyncio.create_task without proper lifecycle management - Do NOT ignore cancellation handling or assume tasks will always complete successfully ## TASK CRITERIA 1. **Event Loop Fundamentals** -- Explain the event loop execution model with visual diagrams described in text, demonstrate how coroutines yield control, and show the difference between awaiting a coroutine directly versus scheduling it as a task 2. **TaskGroup and Structured Concurrency** -- Implement fan-out patterns using asyncio.TaskGroup for parallel HTTP requests, database queries, and file operations with automatic cancellation of remaining tasks when one fails 3. **Semaphore-Based Rate Limiting** -- Build a rate-limited async HTTP client using asyncio.Semaphore to control concurrency, with dynamic semaphore adjustment and fair scheduling for prioritized requests 4. **Producer-Consumer Queue** -- Design a multi-producer multi-consumer pipeline using asyncio.Queue with backpressure, poison pill shutdown, and monitoring for queue depth and processing latency 5. **Async Context Managers and Generators** -- Create resource management patterns with async with for connection pools, implement async generators for streaming data processing, and build async iterators for paginated API consumption 6. **Graceful Shutdown** -- Implement signal handling for SIGTERM and SIGINT that drains in-flight requests, closes connections cleanly, flushes buffers, and provides a configurable timeout before force-killing remaining tasks 7. **Error Handling and Debugging** -- Use ExceptionGroups for TaskGroup failures, implement retry decorators with exponential backoff for transient errors, and configure asyncio debug mode with slow callback detection 8. **Testing Async Code** -- Write pytest-asyncio test cases with async fixtures, mock async dependencies using AsyncMock, and test timing-sensitive code with controlled event loop advancement ## INFORMATION ABOUT ME - My application type: [INSERT YOUR USE CASE, e.g., web scraper, API gateway, real-time data processor, chat server] - My concurrency requirements: [INSERT YOUR TARGET, e.g., 1000 concurrent connections, 50K messages/second] - My external dependencies: [INSERT YOUR I/O TARGETS, e.g., PostgreSQL, Redis, HTTP APIs, WebSocket streams] - My Python version: [INSERT YOUR VERSION, e.g., 3.11, 3.12, 3.13] - My deployment environment: [INSERT YOUR PLATFORM, e.g., single server, Kubernetes pods, serverless] ## RESPONSE FORMAT - Organize patterns from foundational to advanced, building on concepts introduced in earlier sections - Each pattern should include a problem statement, complete code solution, and explanation of when to use it - Include timing benchmarks comparing sync vs async for I/O-bound operations - Provide a decision flowchart in text form for choosing the right concurrency pattern - End with a troubleshooting guide for common async bugs and their debugging strategies
Or press ⌘C to copy