async-python-patterns

Coordinate asynchronous I/O tasks in Python using asyncio.gather, Semaphore, and aiohttp sessions.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/doctorduke/seashells --skill async-python-patterns-doctorduke
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: async-python-patterns
Source: https://github.com/doctorduke/seashells/tree/main/.claude/skills/async-python-patterns
Command: npx skills add https://github.com/doctorduke/seashells --skill async-python-patterns-doctorduke

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires aiohttp, asyncpg, fastapi, pytest-asyncio.

What problem does it solve?

This Skill provides robust patterns for implementing asynchronous I/O operations in Python, dramatically speeding up tasks like web scraping, API calls, and database queries. It helps you overcome performance bottlenecks in I/O-bound code, achieving near-linear scaling with concurrent tasks.

Core Features & Use Cases

  • Parallel I/O Execution: Utilizes asyncio.gather to run multiple I/O-bound tasks (HTTP requests, database queries) concurrently, significantly reducing execution time.
  • Rate Limiting & Error Handling: Implements semaphores for controlled concurrency and includes patterns for graceful error handling and retries.
  • FastAPI Integration: Demonstrates how to build high-performance asynchronous API endpoints using FastAPI.
  • Use Case: You need to fetch data from 100 different API endpoints. Instead of making sequential requests (which would be slow), this skill shows you how to use asyncio.gather and aiohttp to fetch all 100 in parallel, completing the task in roughly the time of a single request.

Quick Start

Example: Fetch multiple URLs in parallel

import asyncio import aiohttp

async def fetch_url(session, url): async with session.get(url) as response: return await response.text()

async def fetch_all(urls): async with aiohttp.ClientSession() as session: tasks = [fetch_url(session, url) for url in urls] results = await asyncio.gather(*tasks) return results

urls = ['https://example.com', 'https://example.org'] results = asyncio.run(fetch_all(urls))

Frequently Asked Questions about async-python-patterns

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

FAQPage Schema
How do I make concurrent HTTP requests in Python without blocking?

Use asyncio.gather with aiohttp to execute multiple HTTP requests in parallel. Create an async function for each request, collect them in a list, and await asyncio.gather to run them concurrently, completing all requests in roughly the time of a single request instead of sequentially.

Can I use asyncio with FastAPI to build high-performance API endpoints?

Yes. FastAPI natively supports async endpoints. Define your route handlers as async functions and use asyncio patterns like asyncio.gather and aiohttp sessions to handle concurrent I/O operations such as parallel database queries or API calls within a single endpoint.

How do I prevent too many concurrent tasks from overwhelming my system?

Implement asyncio.Semaphore to limit concurrency. A semaphore acts as a gate, allowing only a fixed number of tasks to run simultaneously. Wrap your tasks with the semaphore to control throughput and avoid resource exhaustion on rate-limited APIs or database connections.

What's the best way to handle errors in parallel I/O operations?

Use asyncio.gather with return_exceptions=True to capture errors from individual tasks without stopping others. This returns a list mixing successful results and exceptions, letting you inspect and handle failures per task while the batch completes.

Why does my database code still feel slow even with async?

Async eliminates blocking between I/O calls, but you must use async-aware database drivers like asyncpg. Standard synchronous drivers block the event loop. Pair asyncpg with connection pools to reuse database connections across concurrent queries and maximize throughput.

Can I use async patterns for file I/O operations?

Yes. Async patterns apply to file I/O, database queries, and network calls. Use aiohttp for HTTP requests and asyncio.gather to coordinate file reads, writes, and API calls in parallel, achieving near-linear scaling with the number of concurrent tasks.