async-python-patterns

Implement Python asyncio patterns for concurrent, non-blocking applications.

3|1|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/PALabs-v1/AI_friend --skill async-python-patterns-palabs-v1
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: async-python-patterns
Source: https://github.com/PALabs-v1/AI_friend/tree/main/.agents/skills/async-python-patterns
Command: npx skills add https://github.com/PALabs-v1/AI_friend --skill async-python-patterns-palabs-v1

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires aiohttp, pytest-asyncio.

What problem does it solve? Writing concurrent Python code with asyncio is error-prone: forgotten awaits, blocked event loops, unhandled cancellations, and race conditions cause subtle bugs. This Skill provides tested patterns for structuring async code correctly from the start. ## Core Features & Use Cases - Concurrency Patterns: Covers gather(), task creation, semaphores for rate limiting, async locks, and producer-consumer queues. - Real-World Templates: Includes working examples for web scraping with aiohttp, async database access, WebSocket servers, and connection pooling. - Pitfall Avoidance: Documents common mistakes like blocking the event loop, mixing sync and async code, and missing cancellation handling, plus testing with pytest-asyncio. - Use Case: When building a FastAPI service that must call 20 upstream APIs per request, use the semaphore rate-limiting and gather() patterns to run requests concurrently without overwhelming downstream services. ## Quick Start Ask the assistant to write an async Python function that fetches multiple URLs concurrently with rate limiting and proper 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 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 in asyncio?▼

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

Why does my async code block the event loop?▼

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

Can I call an async function from synchronous code?▼

You cannot await inside a regular function. Use asyncio.run() as the entry point from synchronous code on Python 3.7+, which creates the event loop, runs the coroutine, and closes the loop cleanly.

How do I add a timeout to an asyncio operation?▼

Wrap the coroutine in asyncio.wait_for() with a timeout in seconds. If the operation exceeds the limit, it raises asyncio.TimeoutError, which you should catch to handle the failure gracefully.

How do I test async functions with pytest?▼

Use the pytest-asyncio plugin and mark tests with @pytest.mark.asyncio. The test function can then be declared async def and await coroutines directly, including asserting on timeouts and exceptions.