home / skills / athola / claude-night-market / python-async

This skill helps you implement and optimize asynchronous Python patterns for scalable I/O-bound applications and APIs.

npx playbooks add skill athola/claude-night-market --skill python-async

Review the files below or copy the command above to add this skill to your agents.

Files (8)
SKILL.md
2.8 KB
---
name: python-async
description: 'Consult this skill for async Python patterns and concurrency. Use when
  building async APIs, concurrent systems, I/O-bound applications, implementing rate
  limiting, async context managers. Do not use when CPU-bound optimization - use python-performance
  instead. DO NOT use when: testing async code - use python-testing async module.'
category: async
tags:
- python
- async
- asyncio
- concurrency
- await
- coroutines
tools:
- async-analyzer
- concurrency-checker
usage_patterns:
- async-api-development
- concurrent-io
- websocket-servers
- background-tasks
complexity: intermediate
estimated_tokens: 400
progressive_loading: true
modules:
- basic-patterns
- concurrency-control
- error-handling-timeouts
- advanced-patterns
- testing-async
- real-world-applications
- pitfalls-best-practices
---

# Async Python Patterns

asyncio and async/await patterns for Python applications.

## Quick Start

```python
import asyncio

async def main():
    print("Hello")
    await asyncio.sleep(1)
    print("World")

asyncio.run(main())
```
**Verification:** Run the command with `--help` flag to verify availability.

## When To Use

- Building async web APIs (FastAPI, aiohttp)
- Implementing concurrent I/O operations
- Creating web scrapers with concurrent requests
- Developing real-time applications (WebSockets)
- Processing multiple independent tasks simultaneously
- Building microservices with async communication

## When NOT To Use

- CPU-bound optimization - use python-performance
  instead
- Testing async code - use python-testing async module
- CPU-bound optimization - use python-performance
  instead
- Testing async code - use python-testing async module

## Modules

This skill uses progressive loading. Content is organized into focused modules:

- See `modules/basic-patterns.md` - Core async/await, gather(), and task management
- See `modules/concurrency-control.md` - Semaphores and locks for rate limiting
- See `modules/error-handling-timeouts.md` - Error handling, timeouts, and cancellation
- See `modules/advanced-patterns.md` - Context managers, iterators, producer-consumer
- See `modules/testing-async.md` - Testing with pytest-asyncio
- See `modules/real-world-applications.md` - Web scraping and database operations
- See `modules/pitfalls-best-practices.md` - Common mistakes and best practices

Load specific modules based on your needs, or reference all for detailed guidance.

## Exit Criteria

- Async patterns applied correctly
- No blocking operations in async code
- Proper error handling implemented
- Rate limiting configured where needed
- Tests pass with pytest-asyncio
## Troubleshooting

### Common Issues

**Command not found**
Ensure all dependencies are installed and in PATH

**Permission errors**
Check file permissions and run with appropriate privileges

**Unexpected behavior**
Enable verbose logging with `--verbose` flag

Overview

This skill provides concise, practical guidance on async Python patterns and concurrency using asyncio and async/await. It helps implement non-blocking I/O, rate limiting, and common async abstractions for web APIs, scrapers, and real-time systems. Use it to apply safe concurrency patterns and avoid common pitfalls in async code.

How this skill works

The skill explains core constructs (async/await, asyncio.run, tasks, gather) and shows how to manage concurrency with semaphores, locks, and rate limiting. It covers error handling, timeouts, cancellation, async context managers and producer-consumer patterns. Modules focus on targeted topics so you can load only the guidance you need for a particular problem.

When to use it

  • Building async web APIs (FastAPI, aiohttp)
  • Implementing concurrent I/O operations (network, file, DB)
  • Creating web scrapers with many concurrent requests
  • Developing real-time features like WebSockets or push updates
  • Applying rate limiting or concurrency control in services
  • Orchestrating multiple independent I/O-bound tasks concurrently

Best practices

  • Avoid blocking calls inside async code; offload CPU work to threads or separate processes
  • Prefer explicit cancellation and timeouts for long-running tasks
  • Use asyncio.gather and tasks carefully; handle exceptions from grouped coroutines
  • Apply semaphores or token buckets for rate limiting external services
  • Use async context managers to manage resources and ensure cleanup

Example use cases

  • FastAPI endpoint handling many concurrent client connections without blocking
  • A web scraper that issues many HTTP requests concurrently with rate limiting
  • A background worker consuming jobs from a queue with backpressure control
  • Implementing graceful shutdown and cancellation for long-running async tasks
  • Coordinating database queries and I/O operations in an async microservice

FAQ

Can I use asyncio for CPU-bound work?

No. asyncio targets I/O-bound concurrency. For CPU-bound tasks, use multiprocessing or specialized performance tooling (separate worker processes or thread pools).

How do I test async code?

Use pytest-asyncio or equivalent async test frameworks to run coroutines in test contexts and to assert behavior with proper event loop fixtures and simulated timeouts.