Chapter 1. Introduction

Over the last half decade, the tools available to web developers have grown by leaps and bounds. As technologists continue to push the limits of what web applications can do for users everywhere, we’ve had to upgrade our toolkit and create frameworks that let us build better applications. We would like to be able to use new toolkits that make it easier for us to write clean and maintainable code that scales efficiently when deployed to users all across the globe.

This brings us to talking about Tornado, a fantastic choice for writing powerful web applications that are simple to create, extend, and deploy. The three of us had all fallen in love with Tornado for its speed, simplicity, and scalability, and after trying it out on a few personal projects, we’ve put it to work in our day jobs. We’ve seen it increase developer speed (and happiness!) on projects large and small, and at the same time have been impressed time and again by its robustness and lightweight footprint.

This book is meant to be an overview of the Tornado web server, and will walk readers through the basics of the framework, some sample applications, and best practices for use in the real world. We’ll use examples to detail how Tornado works, what you can do with it, and what you’d be best avoiding as you build your first applications with it.

In this book, we’ll be assuming that you have at least a rough understanding of Python, a sense of how web services work, and a basic familiarity with databases. For more on any of those, there are some great books to consult (including Learning Python, Restful Web Services, and MongoDB: The Definitive Guide).

And so you can follow along, the code for the examples in this book is available on Github. If you have any thoughts on these samples or anything else, we’d love to hear from you there.

So, without further ado, let’s dive in!

What Is Tornado?

Tornado is a powerful, scalable web server written in Python. It’s robust enough to handle serious web traffic, yet is lightweight to set up and write for, and can be used for a variety of applications and utilities.

The Tornado we now know is based on a web server framework that was first developed by Bret Taylor and others for FriendFeed, and later open sourced by Facebook when they acquired FriendFeed. Unlike traditional web servers that maxed out at around 10,000 simultaneous connections, Tornado was written with performance in mind, aiming to solve the C10K problem, so by design it’s an extremely high-performance framework. It’s also packed with tools for dealing with security and user authentication, social networks, and asynchronous interaction with external services like databases and web APIs.

Since its release on September 10, 2009, Tornado has garnered a lot of community support, and has been adopted to fit a variety of purposes. In addition to FriendFeed and Facebook, a host of companies have turned to Tornado in production, including Quora, Turntable.fm, Bit.ly, Hipmunk, and MyYearbook, to name a few.

In short, if you’re looking for a replacement for your giant CMS or monolithic development framework, Tornado is probably not the way to go. Tornado doesn’t require that you have giant models set up a particular way, or handle forms in a certain fashion, or anything like that. What it does do is let you write super fast web applications quickly and easily. If you want to create a scalable social application, real-time analytics engine, or RESTful API—all with the power and simplicity of Python—then Tornado (and this book) is for you!

Getting Started with Tornado

Installing Tornado on most *nix systems is easy—you can either get it from PyPI (and install via easy_install or pip), or download the source from Github and build it like this:

$ curl -L -O http://github.com/downloads/facebook/tornado/tornado-2.1.1.tar.gz
$ tar xvzf tornado-2.1.1.tar.gz
$ cd tornado-2.1.1
$ python setup.py build
$ sudo python setup.py install

Tornado is not officially supported on Windows, but it can be installed via ActivePython’s PyPM package manager like so:

C:\> pypm install tornado

Once Tornado is installed on your machine, you’re good to go! A bunch of demos are included with the package, which include examples for building a blog, integrating with Facebook, running a chat server, and more. We’ll be walking through some sample applications step by step later in this book, but be sure to have a look at these later for reference as well.

Caution

We’re assuming for these examples that you are using a Unix-based system and have Python 2.6 or 2.7 installed. If so, you won’t need anything aside from the Python standard library. You can run Tornado under Python 2.5 provided you have installed pycURL, simpleJSON, and the Python development headers, and on Python 3.2 with the distribute package. However, you should note that Python 3+ support is new as of Tornado 2.0, and the Tornado team has advised developers to continue to keep an eye out for bugs on that front.

Community and Support

For questions, examples, and general how-to’s, the official Tornado documentation is a great place to start. There’s a variety of examples and breakdowns of features at tornadoweb.org, and more specific details and changes can be seen at Facebook’s Tornado repository on Github. For more specific concerns, the Tornado Web Server Google Group is active and full of folks who use Tornado on a daily basis.

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.