Friday 3:15 p.m.–4 p.m.
What Is Async, How Does It Work, And When Should I Use It?
- Audience level:
- Web Frameworks
Python’s asynchronous frameworks, like Tulip, Tornado, and Twisted, are increasingly important for writing high-performance web applications. Even if you’re an experienced web programmer, you may lack a rigorous understanding of how these frameworks work and when to use them. Let’s see how Tulip's event loop works, and learn how to efficiently handle very large numbers of concurrent connections.
**I. Why Async?** A. First, There Was Multithreading: Traditional web servers use a thread per connection to store state. B. Trends: Contemporary web applications trend towards using web sockets and service-oriented architectures, which require very high numbers of concurrent connections. C. Limitations of Multithreading: Per-thread overhead limits the number of connections possible. We need a more efficient way to store state. **II. What Is Async?** A. Single-threaded: Asynchronous frameworks support high concurrency, typically with a single thread. B. Event-driven: Async frameworks use non-blocking sockets, and register for notifications on them using EPoll, Kqueue, or similar. C. State: The techniques used to store state—coroutines, callbacks, Deferreds—are more memory-efficient than threads but unfamiliar to most programmers. **III. How Does Async Work?** A. Frameworks: There are a number of asynchronous frameworks available for Python. The most popular are Tornado and Twisted. This year Guido has led development on an async framework for Python’s standard library, codenamed Tulip. Tulip is included in the Python 3.4 standard library as "asyncio." B. Finally, Some Code: Look at a chat server written in the multithreaded style with Flask, versus an async server that uses Tulip. The multithreaded code implicitly stores its per-connection state in each thread’s stack, stack pointer, and program counter. In Tulip those concepts map to generators, references to generators, and generators’ instruction pointers. C. The Tulip Event Loop: A stroll through the code for Tulip’s event loop. Tulip uses non-blocking sockets and Epoll / Kqueue to perform concurrent operations in a single thread. D. The Worst Sin: Any synchronous code in an async app blocks the event loop and makes the application crawl. When you grasp why, you’re way ahead of most programmers using Python async frameworks. **IV. When Should I Use It?** A. Indications: High connection counts, server-side events pushed to clients, leisure to code against a more challenging API. B. Contraindications: Reliance on synchronous third-party libraries, relaxed performance requirements.