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))