async-python-patterns

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

Updated Nov 1, 2024
One-click install
npx skills add https://github.com/mlorentedev/dotfiles --skill async-python-patterns-mlorentedev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: async-python-patterns
Source: https://github.com/mlorentedev/dotfiles/tree/main/harness/skills/async-python-patterns
Command: npx skills add https://github.com/mlorentedev/dotfiles --skill async-python-patterns-mlorentedev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing concurrent Python code often leads to blocking event loops, forgotten awaits, and unhandled task failures. This Skill provides proven asyncio patterns and decision guidance so you can build correct, high-performance async applications without common pitfalls. ## Core Features & Use Cases - Sync vs Async Decision Guide: A comparison table helps you choose between asyncio, multiprocessing, thread pools, or plain sync code based on your workload type. - Ready-to-Use Patterns: Copy-paste examples for concurrent execution with gather, task management with create_task, batch error handling with return_exceptions, and timeouts with wait_for. - Pitfall Prevention: Explicit coverage of blocking the event loop, missing await keywords, CancelledError handling, and sync/async boundary management. - Use Case: You are building a FastAPI service that must call three external APIs per request. Use the concurrent execution pattern to fire all calls with asyncio.gather, apply per-call timeouts, and collect partial results without crashing the batch. ## Quick Start Ask the AI to refactor a slow synchronous Python script that makes sequential HTTP requests into a concurrent asyncio implementation using gather and proper timeout 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 coroutines concurrently and collect their results, or asyncio.create_task to schedule tasks explicitly. On Python 3.11+, prefer asyncio.TaskGroup for supervised tasks that cancel siblings on failure.

When should I use asyncio vs multiprocessing in Python?

Use asyncio for I/O-bound work like concurrent network or database calls, and multiprocessing for CPU-bound computation. For mixed workloads, offload CPU work from async code with asyncio.to_thread or a thread pool.

Why is my async Python code still running slowly?

The event loop is likely blocked by synchronous calls like time.sleep or blocking I/O libraries. Replace them with await asyncio.sleep and async-native libraries, or wrap unavoidable blocking calls in asyncio.to_thread.

How do I add a timeout to an async function in Python?

Wrap the coroutine with asyncio.wait_for and pass a timeout in seconds, catching asyncio.TimeoutError when it expires. On Python 3.11+, asyncio.timeout provides a cleaner context-manager alternative.

How do I test async Python code with pytest?

Mark async test functions with @pytest.mark.asyncio so pytest executes them on an event loop. You can then await coroutines directly and assert on results or expected exceptions like asyncio.TimeoutError.