async-python-patterns

Implement asyncio concurrency patterns for non-blocking Python applications and async APIs.

Updated Nov 30, 2025
One-click install
npx skills add https://github.com/Amakaflow/amakaflow-dev-workspace --skill async-python-patterns-amakaflow
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: async-python-patterns
Source: https://github.com/Amakaflow/amakaflow-dev-workspace/tree/main/.claude/skills/async-python-patterns
Command: npx skills add https://github.com/Amakaflow/amakaflow-dev-workspace --skill async-python-patterns-amakaflow

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing concurrent Python code is error-prone: blocking calls stall the event loop, unbounded concurrency exhausts resources, and mixing sync and async code creates hidden bugs. This Skill provides tested asyncio patterns that prevent these failures. ## Core Features & Use Cases - Concurrency Patterns: Ready-to-use implementations of gather(), task management, semaphores for rate limiting, producer-consumer queues, and async locks. - Real-World Recipes: Complete examples for web scraping with aiohttp, async database operations, WebSocket servers, and connection pooling. - Pitfall Prevention: Guidance on avoiding blocking calls, handling task cancellation, wrapping sync code with asyncio.to_thread(), and testing with pytest-asyncio. - Use Case: When building a FastAPI service that must call 20 external APIs per request, use the semaphore rate-limiting pattern to bound concurrency and the timeout pattern to prevent hanging requests. ## Quick Start Ask the AI to show you how to fetch multiple URLs concurrently with rate limiting using asyncio and aiohttp.

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 as a list. For long-running work, create tasks with asyncio.create_task() and await them later, which lets the event loop interleave execution.

How to limit concurrent requests with asyncio semaphore?

Create an asyncio.Semaphore with a maximum count and wrap each operation in 'async with semaphore'. This bounds how many coroutines run simultaneously, preventing resource exhaustion when calling external APIs at scale.

Should I use asyncio or multiprocessing for my Python application?

Use asyncio for I/O-bound work like network and database calls with many concurrent connections. Use multiprocessing for CPU-bound computation, since asyncio runs on a single thread and cannot parallelize CPU work.

Can I call synchronous libraries inside async functions?

Yes, wrap blocking calls with asyncio.to_thread() (Python 3.9+) to run them in a thread pool without stalling the event loop. Calling sync code directly inside a coroutine blocks all concurrent tasks.

Why does my async code hang or time out?

Hangs usually come from forgetting await, blocking the event loop with time.sleep() or requests, or operations without timeouts. Use asyncio.wait_for() to enforce deadlines and asyncio.sleep() instead of time.sleep().

How do I test async functions with pytest?

Use the pytest-asyncio plugin and mark test functions with @pytest.mark.asyncio. Tests can then await coroutines directly, including asserting that asyncio.TimeoutError is raised for slow operations.