Python Asyncio & Asynchronous Programming

Build high-performance asynchronous applications with Python asyncio

# Python Asyncio

This document provides comprehensive guidelines for python asyncio development and best practices.

---

## Asyncio Fundamentals

1. **Event**
   - Event loop architecture
   - Implement proper event loop architecture
   - Follow best practices for optimal results

2. **Coroutines**
   - Coroutines and async/await syntax
   - Implement proper coroutines and async/await syntax
   - Follow best practices for optimal results

3. **Tasks**
   - Tasks for concurrent execution
   - Implement proper tasks for concurrent execution
   - Follow best practices for optimal results

4. **Futures**
   - Futures for result management
   - Implement proper futures for result management
   - Follow best practices for optimal results

5. **Asyncio**
   - Asyncio ecosystem overview
   - Implement proper asyncio ecosystem overview
   - Follow best practices for optimal results

---

## Coroutines & Tasks

6. **async**
   - async def function definitions
   - Implement proper async def function definitions
   - Follow best practices for optimal results

7. **await**
   - await expressions for suspension
   - Implement proper await expressions for suspension
   - Follow best practices for optimal results

8. **Task**
   - Task creation with create_task()
   - Implement proper task creation with create_task()
   - Follow best practices for optimal results

9. **Task**
   - Task cancellation and timeout handling
   - Implement proper task cancellation and timeout handling
   - Follow best practices for optimal results

10. **Exception**
   - Exception handling in coroutines
   - Implement proper exception handling in coroutines
   - Follow best practices for optimal results

---

## Event Loop Management

11. **Event**
   - Event loop creation and running
   - Implement proper event loop creation and running
   - Follow best practices for optimal results

12. **asyncio.run()**
   - asyncio.run() for simple cases
   - Implement proper asyncio.run() for simple cases
   - Follow best practices for optimal results

13. **Loop**
   - Loop scheduling and callbacks
   - Implement proper loop scheduling and callbacks
   - Follow best practices for optimal results

14. **Thread-safe**
   - Thread-safe operations
   - Implement proper thread-safe operations
   - Follow best practices for optimal results

15. **Custom**
   - Custom event loop policies
   - Implement proper custom event loop policies
   - Follow best practices for optimal results

---

## Concurrent Programming

16. **asyncio.gather()**
   - asyncio.gather() for parallel execution
   - Implement proper asyncio.gather() for parallel execution
   - Follow best practices for optimal results

17. **asyncio.wait()**
   - asyncio.wait() for completion handling
   - Implement proper asyncio.wait() for completion handling
   - Follow best practices for optimal results

18. **Semaphores**
   - Semaphores for resource limiting
   - Implement proper semaphores for resource limiting
   - Follow best practices for optimal results

19. **Locks**
   - Locks and synchronization primitives
   - Implement proper locks and synchronization primitives
   - Follow best practices for optimal results

20. **Queue**
   - Queue patterns for producer-consumer
   - Implement proper queue patterns for producer-consumer
   - Follow best practices for optimal results

---

## Network Programming

21. **HTTP**
   - HTTP client with aiohttp
   - Implement proper http client with aiohttp
   - Follow best practices for optimal results

22. **WebSocket**
   - WebSocket connections
   - Implement proper websocket connections
   - Follow best practices for optimal results

23. **TCP/UDP**
   - TCP/UDP server and client
   - Implement proper tcp/udp server and client
   - Follow best practices for optimal results

24. **SSL/TLS**
   - SSL/TLS support
   - Implement proper ssl/tls support
   - Follow best practices for optimal results

25. **Connection**
   - Connection pooling strategies
   - Implement proper connection pooling strategies
   - Follow best practices for optimal results

---

## File I/O Operations

26. **aiofiles**
   - aiofiles for file operations
   - Implement proper aiofiles for file operations
   - Follow best practices for optimal results

27. **Asynchronous**
   - Asynchronous file reading/writing
   - Implement proper asynchronous file reading/writing
   - Follow best practices for optimal results

28. **Directory**
   - Directory operations
   - Implement proper directory operations
   - Follow best practices for optimal results

29. **Subprocess**
   - Subprocess management
   - Implement proper subprocess management
   - Follow best practices for optimal results

30. **Stream**
   - Stream processing
   - Implement proper stream processing
   - Follow best practices for optimal results

---

## Database Integration

31. **asyncpg**
   - asyncpg for PostgreSQL
   - Implement proper asyncpg for postgresql
   - Follow best practices for optimal results

32. **aiomysql**
   - aiomysql for MySQL
   - Implement proper aiomysql for mysql
   - Follow best practices for optimal results

33. **aioredis**
   - aioredis for Redis
   - Implement proper aioredis for redis
   - Follow best practices for optimal results

34. **Motor**
   - Motor for MongoDB
   - Implement proper motor for mongodb
   - Follow best practices for optimal results

35. **Connection**
   - Connection pooling and transactions
   - Implement proper connection pooling and transactions
   - Follow best practices for optimal results

---

## Web Development

36. **FastAPI**
   - FastAPI for async web APIs
   - Implement proper fastapi for async web apis
   - Follow best practices for optimal results

37. **aiohttp**
   - aiohttp for web applications
   - Implement proper aiohttp for web applications
   - Follow best practices for optimal results

38. **WebSocket**
   - WebSocket handling
   - Implement proper websocket handling
   - Follow best practices for optimal results

39. **Middleware**
   - Middleware implementation
   - Implement proper middleware implementation
   - Follow best practices for optimal results

40. **Request/response**
   - Request/response streaming
   - Implement proper request/response streaming
   - Follow best practices for optimal results

---

## Error Handling

41. **Exception**
   - Exception propagation in async code
   - Implement proper exception propagation in async code
   - Follow best practices for optimal results

42. **Timeout**
   - Timeout handling with asyncio.wait_for()
   - Implement proper timeout handling with asyncio.wait_for()
   - Follow best practices for optimal results

43. **Cancellation**
   - Cancellation and cleanup
   - Implement proper cancellation and cleanup
   - Follow best practices for optimal results

44. **Retry**
   - Retry mechanisms
   - Implement proper retry mechanisms
   - Follow best practices for optimal results

45. **Circuit**
   - Circuit breaker patterns
   - Implement proper circuit breaker patterns
   - Follow best practices for optimal results

---

## Performance Optimization

46. **Profiling**
   - Profiling async applications
   - Implement proper profiling async applications
   - Follow best practices for optimal results

47. **Memory**
   - Memory usage optimization
   - Implement proper memory usage optimization
   - Follow best practices for optimal results

48. **CPU-bound**
   - CPU-bound task handling
   - Implement proper cpu-bound task handling
   - Follow best practices for optimal results

49. **Backpressure**
   - Backpressure management
   - Implement proper backpressure management
   - Follow best practices for optimal results

50. **Resource**
   - Resource cleanup strategies
   - Implement proper resource cleanup strategies
   - Follow best practices for optimal results

---

## Testing Async Code

51. **pytest-asyncio**
   - pytest-asyncio for async tests
   - Implement proper pytest-asyncio for async tests
   - Follow best practices for optimal results

52. **Mock**
   - Mock async functions
   - Implement proper mock async functions
   - Follow best practices for optimal results

53. **Integration**
   - Integration testing patterns
   - Implement proper integration testing patterns
   - Follow best practices for optimal results

54. **Performance**
   - Performance testing
   - Implement proper performance testing
   - Follow best practices for optimal results

55. **Debugging**
   - Debugging async applications
   - Implement proper debugging async applications
   - Follow best practices for optimal results

---

## Advanced Patterns

56. **Context**
   - Context managers with async with
   - Implement proper context managers with async with
   - Follow best practices for optimal results

57. **Async**
   - Async generators and iterators
   - Implement proper async generators and iterators
   - Follow best practices for optimal results

58. **AsyncIterator**
   - AsyncIterator protocol implementation
   - Implement proper asynciterator protocol implementation
   - Follow best practices for optimal results

59. **Custom**
   - Custom awaitable objects
   - Implement proper custom awaitable objects
   - Follow best practices for optimal results

60. **Protocol-based**
   - Protocol-based programming
   - Implement proper protocol-based programming
   - Follow best practices for optimal results

---

## Integration with Sync Code

61. **asyncio.to_thread()**
   - asyncio.to_thread() for blocking calls
   - Implement proper asyncio.to_thread() for blocking calls
   - Follow best practices for optimal results

62. **Executor**
   - Executor usage for CPU-bound tasks
   - Implement proper executor usage for cpu-bound tasks
   - Follow best practices for optimal results

63. **Thread**
   - Thread pool integration
   - Implement proper thread pool integration
   - Follow best practices for optimal results

64. **Process**
   - Process pool for parallel processing
   - Implement proper process pool for parallel processing
   - Follow best practices for optimal results

65. **Bridging**
   - Bridging sync and async libraries
   - Implement proper bridging sync and async libraries
   - Follow best practices for optimal results

---

## Production Considerations

66. **Event**
   - Event loop monitoring
   - Implement proper event loop monitoring
   - Follow best practices for optimal results

67. **Resource**
   - Resource leak detection
   - Implement proper resource leak detection
   - Follow best practices for optimal results

68. **Graceful**
   - Graceful shutdown handling
   - Implement proper graceful shutdown handling
   - Follow best practices for optimal results

69. **Process**
   - Process management
   - Implement proper process management
   - Follow best practices for optimal results

70. **Container**
   - Container deployment strategies
   - Implement proper container deployment strategies
   - Follow best practices for optimal results

---

## Common Pitfalls

71. **Blocking**
   - Blocking the event loop
   - Implement proper blocking the event loop
   - Follow best practices for optimal results

72. **Resource**
   - Resource cleanup issues
   - Implement proper resource cleanup issues
   - Follow best practices for optimal results

73. **Exception**
   - Exception handling mistakes
   - Implement proper exception handling mistakes
   - Follow best practices for optimal results

74. **Task**
   - Task cancellation problems
   - Implement proper task cancellation problems
   - Follow best practices for optimal results

75. **Memory**
   - Memory leak prevention
   - Implement proper memory leak prevention
   - Follow best practices for optimal results

---

## Summary Checklist

- [ ] Core principles implemented
- [ ] Best practices followed
- [ ] Performance optimized
- [ ] Security measures in place
- [ ] Testing strategy implemented
- [ ] Documentation completed
- [ ] Monitoring configured
- [ ] Production deployment ready

---

Follow these comprehensive guidelines for successful python asyncio implementation.
Python Asyncio & Asynchronous Programming - Cursor IDE AI Rule