async-python-patterns
掌握 Python 非同步模式
Auch verfügbar von: wshobson
在 Python 中建立非同步應用程式需要了解複雜的並發執行、工作管理和效能最佳化模式。本技能提供關於 asyncio 基礎知識、現實世界的模式和最佳實踐的全面指導,以建立高效能的非阻塞系統。
Die Skill-ZIP herunterladen
In Claude hochladen
Gehe zu Einstellungen → Fähigkeiten → Skills → Skill hochladen
Einschalten und loslegen
Teste es
Verwendung von "async-python-patterns". How do I prevent blocking the event loop when running CPU-intensive tasks in async Python code?
Erwartetes Ergebnis:
- Use asyncio.to_thread() (Python 3.9+) or loop.run_in_executor() to offload blocking CPU work to a thread pool
- Never use time.sleep() in async code - use await asyncio.sleep() instead
- Example pattern: wrap CPU-intensive functions with run_in_executor to keep the event loop responsive
Verwendung von "async-python-patterns". What is the difference between asyncio.create_task() and asyncio.gather()?
Erwartetes Ergebnis:
- create_task() schedules a single coroutine to run soon, returning a Task object you can await later
- gather() runs multiple coroutines concurrently and waits for all to complete, returning results as a list
- Use create_task() when you need to manage individual tasks; use gather() for fire-and-forget concurrent execution
Verwendung von "async-python-patterns". How do I implement a timeout for an async function that might hang?
Erwartetes Ergebnis:
- Use asyncio.wait_for() to wrap your async call with a timeout value
- Catch asyncio.TimeoutError to handle timeout scenarios gracefully
- Example: result = await asyncio.wait_for(async_operation(), timeout=5.0)
Sicherheitsaudit
SicherPure educational documentation containing Python asyncio code examples. All static findings are false positives from the analyzer misinterpreting markdown code blocks and documentation text. No executable code, scripts, network calls, file operations, or external commands exist in this skill.
Risikofaktoren
⚙️ Externe Befehle (56)
🌐 Netzwerkzugriff (10)
📁 Dateisystemzugriff (1)
Qualitätsbewertung
Was du bauen kannst
建立非同步 Web API
學習使用 FastAPI、aiohttp 或 Sanic 建立高效能非同步 API 的模式
並發 I/O 操作
實現高效的並發資料庫、檔案和網路操作,而不會發生阻塞
即時應用程式
開發 WebSocket 伺服器、聊天系統和事件驅動的應用程式
Probiere diese Prompts
Show me how to use asyncio.gather() to run multiple async functions concurrently in Python
How do I properly handle errors and timeouts when using asyncio.wait_for() in Python
Explain how to use asyncio.Semaphore to rate limit concurrent API calls in Python
Show me an example of the producer-consumer pattern using asyncio.Queue for processing items concurrently
Bewährte Verfahren
- Always use asyncio.run() as the entry point for async programs (Python 3.7+)
- Use asyncio.gather() for concurrent execution of independent async operations
- Implement proper timeout handling with asyncio.wait_for() to prevent hanging operations
Vermeiden
- Calling async functions without await - this returns a coroutine object instead of executing the function
- Using blocking operations like time.sleep() or CPU-intensive work directly in async code
- Mixing synchronous and asynchronous code without proper translation using asyncio.run()