async-python-patterns

Implement asyncio concurrency patterns for non-blocking Python applications.

Updated Jul 29, 2026
One-click install
npx skills add https://github.com/MaiconGambini/opencode-harness-guide --skill async-python-patterns-maicongambini
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: async-python-patterns
Source: https://github.com/MaiconGambini/opencode-harness-guide/tree/main/skills/async-python-patterns
Command: npx skills add https://github.com/MaiconGambini/opencode-harness-guide --skill async-python-patterns-maicongambini

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires aiohttp, pytest-asyncio.

What problem does it solve? Writing concurrent Python code with asyncio is error-prone: developers forget awaits, block the event loop, mishandle cancellation, or lack patterns for rate limiting and backpressure. This Skill provides structured guidance and ready-to-adapt patterns for building correct async applications. ## Core Features & Use Cases - Concurrency Patterns: Covers gather(), task creation, producer-consumer queues, semaphores for rate limiting, and async locks for shared state. - Robustness Guidance: Includes timeout handling, structured error handling with gather(return_exceptions=True), and proper cancellation cleanup. - Real-World Examples: Demonstrates web scraping with aiohttp, concurrent database queries, WebSocket servers, and connection pooling. - Use Case: When building a FastAPI service that must call 20 upstream APIs per request, use the semaphore and gather patterns to run requests concurrently with controlled rate limits and timeouts. ## Quick Start Ask the AI to show how to fetch multiple URLs concurrently in Python with asyncio, including timeouts, rate limiting, and error handling.

Frequently Asked Questions about async-python-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I run multiple async tasks concurrently in Python?

Use asyncio.gather() to run multiple coroutines concurrently and collect their results in order. For long-running work, create tasks with asyncio.create_task() and await them when needed.

How to limit concurrent requests with asyncio?

Use asyncio.Semaphore to cap concurrency. Wrap each operation in 'async with semaphore' so only a fixed number of tasks run at once, which is the standard pattern for rate limiting API calls.

When should I not use asyncio in Python?

Avoid asyncio for CPU-bound workloads, since the single-threaded event loop gains nothing there; use multiprocessing instead. It is also unnecessary for simple synchronous scripts or environments that cannot run an event loop.

Why does my async code block the event loop?

Blocking happens when synchronous calls like time.sleep() or CPU-heavy work run inside a coroutine. Replace them with asyncio.sleep() or offload blocking work to a thread pool via loop.run_in_executor().

How do I add timeouts to async operations in Python?

Wrap the coroutine with asyncio.wait_for(coro, timeout=seconds). It raises asyncio.TimeoutError when the deadline is exceeded, which you can catch to handle slow operations gracefully.

How do I test async Python code with pytest?

Use the pytest-asyncio plugin and mark tests with @pytest.mark.asyncio so async test functions run on an event loop. You can then await coroutines directly and assert on results or expected exceptions like TimeoutError.