Talks - Arun Suresh Kumar: Sync vs. Async in Python: Tools, Benchmarks, and ASGI/WSGI Explained

Explore Python's ASGI/WSGI protocols, benchmarking strategies, and best practices for async programming. Learn when to choose sync vs. async for optimal application performance.

Key takeaways
  • ASGI handles multiple concurrent connections more efficiently than WSGI, allowing better scaling and performance for high-traffic applications

  • When migrating from sync to async:

    • Core functionality should be non-blocking
    • Careful consideration needed for library choices as not all support true async
    • Memory usage typically increases but handles more concurrent users
    • Use native async libraries when possible for best performance
  • Benchmarking insights:

    • WSGI typically handles ~100 concurrent users
    • ASGI can handle 1000+ users with higher requests per second (1333.5 RPS)
    • Docker/container environments require different benchmarking approaches than bare metal
    • Watch for unexpected performance dips from remaining sync code
  • Key Python async evolution milestones:

    • Python 3.4: Native async support introduced
    • Python 3.5: async/await keywords added
    • Modern frameworks like FastAPI, Django, and Falcon support both WSGI and ASGI
  • Best practices:

    • Use Task Groups for handling multiple concurrent functions
    • Consider WebSocket/HTTP/2 for long-polling communications
    • Monitor memory and CPU utilization carefully
    • Scale horizontally and vertically based on workload
    • Use async versions of ORMs and database drivers when available
  • Common frameworks compatibility:

    • Django: Supports both WSGI and ASGI
    • FastAPI: ASGI only
    • Flask: WSGI
    • Falcon: Supports both
    • Tornado: ASGI support