Chapter 5. Asynchronous Web Services

Thus far, we’ve taken a look at many of the features that make Tornado such a powerful framework for web applications. Its simplicity, ease of use, and handy helpers are enough reason to make it a great choice for many web projects. However, one of the most talked about features of Tornado is its ability to fetch and serve content asynchronously, and with good reason: it makes it easy to handle nonblocking requests, ultimately resulting in more efficient processes and greater scaling possibilities. In this chapter, we’ll take a look at the basics of asynchronous requests in Tornado, as well as some long polling techniques that will allow you to write simpler web applications that can serve more requests with fewer resources.

Asynchronous Web Requests

Most web applications (including the examples we’ve looked at thus far) are blocking in nature, meaning that while a request is being handled, the process hangs until the request is completed. In most cases, web requests handled by Tornado should complete fast enough that this is not a concern. However, for operations that can take some time to complete (like large database requests or calls to external APIs) it means that the application is effectively locked until the process is finished, and for obvious reasons that is a problem at scale.

However, Tornado gives us better ways to handle this sort of situation. Instead of leaving a process hanging while it waits for a request to finish, the application ...

Get Introduction to Tornado now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.