Agent skill
python-async
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/python-async
SKILL.md
Python Async
Overview
Asyncio is a concurrency model designed for IO-bound and high-level structured network code. It uses cooperative multitasking on a single thread, allowing routines to pause while waiting for I/O, yielding control back to the event loop.
When to Use
- Network IO: Web scraping, API requests, and database queries.
- Web Servers: Handling thousands of concurrent connections (e.g., FastAPI).
- Concurrent Tasks: Running multiple independent IO-bound operations simultaneously.
Decision Tree
- Is the task CPU-bound (e.g., heavy math, image processing)?
- YES: Use
multiprocessinginstead. - NO: Is the task waiting for external resources (API, Disk)?
- YES: Use
asyncio.
- YES: Use
- YES: Use
- Are you using
time.sleep?- YES: Replace with
await asyncio.sleepto avoid blocking the event loop.
- YES: Replace with
Workflows
1. Concurrent Task Execution
- Define multiple coroutine functions with
async def. - Initiate the tasks using
asyncio.gather(*coros)to run them concurrently. - Await the results to aggregate outputs efficiently.
2. Asynchronous Resource Management
- Implement an asynchronous context manager using
__aenter__and__aexit__. - Use the
async withsyntax to ensure resources (like network connections) are opened and closed without blocking the loop. - Perform I/O operations inside the context using
await.
3. Processing Async Streams
- Create an asynchronous generator using
yieldinside anasync deffunction. - Iterate over the generator using
async forto process data as it becomes available. - Avoid materializing the entire sequence in memory to keep memory overhead low.
Non-Obvious Insights
- Cooperative Multitasking: Async is NOT parallelism; it is single-threaded. If one coroutine blocks (e.g.,
time.sleep), the entire program stops. - Modern Entry Point: Always use
asyncio.run(main())for the main entry point; avoid manual event loop management in modern Python. - Materialization Risk: Using
async foris essential for large datasets to prevent OOM (Out of Memory) errors by processing items one by one as they arrive.
Evidence
- "asyncio is often a perfect fit for IO-bound and high-level structured network code." - Python Docs
- "Async I/O is a single-threaded, single-process technique that uses cooperative multitasking." - Real Python
- "The await keyword suspends the execution of the surrounding coroutine and passes control back to the event loop." - Real Python
Scripts
scripts/python-async_tool.py: Examples ofasyncio.gatherandasync forgenerators.scripts/python-async_tool.js: Equivalent JavaScriptPromise.alland async iterator examples.
Dependencies
asyncio(Standard Library)aiohttporhttpx(Recommended for async HTTP)
References
- references/README.md
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?