Skip to main content

What is Async, How Does it Work, and When Should I Use it?

Average rating: ****.
(4.33, 3 ratings)
Slides:   1-PDF 

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. Recently Python’s inventor Guido van Rossum has led development on an async framework for Python’s standard library, named Tulip or “asyncio”.

B. Finally, Some Code: Look at a chat server written in the multithreaded style with Flask, versus an async server that uses Tornado. The multithreaded code implicitly stores its per-connection state in each thread’s stack, stack pointer, and program counter. In Tornado those concepts map to callbacks and the “self” object.

C. The Tornado Event Loop: A stroll through the code for Tornado event loop. Tornado uses non-blocking sockets and Epoll or 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.

Photo of A. Jesse Jiryu Davis

A. Jesse Jiryu Davis


Senior Python Engineer at MongoDB in New York City. Author of Motor, an async MongoDB driver for Tornado, and of Toro, a library of locks and queues for Tornado coroutines. Contributor to Python, PyMongo, MongoDB, Tornado, and Tulip.