Tornado: A Python Web Framework That’s Simple and Quick

0
248
Python Program to Help You Backup Files Automatically
Python visual
This article presents Tornado, the Python Web framework. Originally developed at FriendFeed, Tornado is an asynchronous networking library. It is able to scale up to tens of thousands of open connections with the help of its non-blocking network I/O

Apart from introducing you to Tornado, this article explores the extent to which it can be scaled, its features, the installation process, and its advantages compared to other Python Web frameworks. This article is meant for those interested in knowing more about Web designing as well as Web developers. It may even be helpful to serious Python developers or contributors trying to explore ways to improve their Web development skills.

A framework is a collection of programs that uses a control mechanism to structure code to build a bigger system. We can develop simple websites using basic designing languages like HTML, CSS, Python, etc. However, considering the complex and dynamic nature of the Web, it is difficult to do this at a low cost and within the short deadlines that sites are typically developed. Hence, developers want something that empowers them and has fewer bugs. The purpose of a Web framework is to provide some features that are essential for all kinds of applications, such as database support, URL handling, HTML templating, etc.

Python is one of the most used general-purpose programming languages. It is also used as a scripting language but mostly in non-scripting contexts. Python’s environment consists of a number of Web frameworks, ranging from the minimalistic to the fully bloated. Tornado is one of the best Web frameworks of the Python programming language. It is Facebook’s real time Python Web framework (we will go into this later). It enables a clean approach to Web server programming, focuses on non-blocking operations and scales to an enormous number of open connections. Tornado is an asynchronous networking library that was originally developed at FriendFeed, a kind of social network that enables you to share the issues you found on the Web and it allows discussions. FriendFeed was acquired by Facebook in 2009 and Tornado was open sourced soon after. Django, CherryPy, web2Py, Grok, Pylons and TurboGears are some of the other frameworks.

Why Tornado?
So why is Tornado preferred, even though Python has a number of frameworks? Well, Tornado is noted for its high performance and supposedly has the capacity to solve the C10k problem (problem of optimising network related issues so that it can handle a large number of clients at the same time) that affects other servers. Tornado focuses on speed and handling large volumes of simultaneous traffic.

Let’s look at those features of Tornado that differentiate it from other frameworks.

Built-in support: Tornado comes with built-in support and finds solutions for most of the tedious aspects of Web development like templates, localisation, signed cookies, etc. Tornado also allows users to mix it with other frameworks, with matching snippets, according to their needs.

Concurrent connections: Tornado offers real time services and supports a large number of concurrent connections, HTTP streaming (a communication protocol implemented by Apple Inc) and long polling (it is a ‘push’ technology, which allows an emulative push mechanism under circumstances in which a real push is not possible). With Tornado, it is very easy to write real time services. FriendFeed maintains an open connection, especially for its frequently involved users.
High performance: This is Tornado’s most attractive feature. It is very fast compared to all other Python Web frameworks. Considering its baseline output, it is around four times higher in speed and pretty efficient also.

Getting started with Tornado
Tornado platforms: Tornado runs on most of the platforms like UNIX, Windows, and so on. For better performance and scalability, only Linux with EPoll (a Linux kernel system call which replaced older calls to achieve better performance), and BSD (Berkeley Standard Distribution) with KQueue (an event-notification interface) are recommended for effective production deployment. Windows is officially not supported but is recommended only for development purposes.
Installation: Tornado is listed in PyPI, which is a Python Package Index—a repository of software for the Python programming language. As Tornado is listed in PyPI, it can be installed easily using Pip:

pip install tornado

As an example, let’s create a new project called webappdemo. We have the app.py python file whose contents are as follows:
_app_: It is the main method for Python to start executing and is the entry for the program.

tornado.ioloop: The ioloop class is the main event for any application which can create a non-blocking thread for an application.

tornado.web: This is the basic module of Tornado, which implements a lightweight Web development framework. It was built in FriendFeed using their non-blocking HTTP server and low-level I/O modules. In this demo, MainHandler is the single handler mapped to the root path.

MainHandler: This is a handler for a request, which refers to the self.write() method. The MainHandler class returns the response using this method.
application listen (8888): We look at the specification 8888 first. It is just a port which the application considers (i.e., listens to). Any one can communicate with this application through this particular port (8888).
It’s now time to run the application. You can run this simple application in a terminal (if you are using Ubuntu). If you are using the Windows platforms, you can run this application using the command prompt.
To run your application in a terminal, use:

python app.py

Finally, you should be able to visit your website at http://localhost:8888

Figure 1
Tornado and other Python Web frameworks

Pyramid, Flask, Bottle and Django are among the well known frameworks of Python.
Pyramid is a small framework which makes real-world Web applications productive. Its advantages over other frameworks are:

  • A quick start
  • Flexibility
  • Compatible with Python 3
  • Ability to develop large Web applications, etc.

Flask is a micro framework of Python which is easy to set up and comes with a built-in server. Flask’s main features are:

  • It implements most commonly used core components
  • Provides users a choice for database access or validation
  • Flask extensions are available

Bottle is a framework that provides minimal tools out-of-the-box (routing, templating, etc).
It also comes under the category of ‘micro’ , which enables compilation in Python 3. Some of the other features of Bottle are:

  • Ability to build simple projects
  • Efficient in creating Web application interfaces
  • Flexibility

Django is a powerful Web framework that is supported by a large and active community. It offers model-based forms and has its own excellent documentation. Django makes it easier to build better Web applications, quickly and with less code. It is a high-level Python Web framework that encourages rapid development and perfect design.
Some of the attractive features of Django are:

  • Predefined powerful admin interface
  • Offers model-based forms
  • Django extensions are available
  • Has useful applications

The purpose of a Web framework is to provide some common features that are essential for all kinds of applications. The Model-View-Controller (or MVC) is software for implementing user interfaces and is the de-facto standard for developing Web applications. Both Tornado and Django provide some form of database support, HTML templating, domain handling, etc.

Tornado vs Django
Even though both Tornado and Django provide the MVC architecture, they are at opposite ends of the spectrum.

Django is more mature and is one among the largest Web-based frameworks for Python. It has a lot of features and takes care of the details for the user, so that you do not need to understand all the basics when making a fully-featured website. It is stable and user friendly too. But Django is very hard to customise and troubleshoot, in some situations, due to its all-encompassing nature. This is one of its drawbacks.

Tornado is lighter in weight and has a few more features than Django. As I said earlier, Tornado is known for its high performance. It runs faster and provides an asynchronous IOLoop for polling requests, and a variety of oauth and social media features. It is small in size and runs fast. And it is customisable to the tiniest detail.

Django actually fits most of the platforms. HTTP requests are a fundamental part of every Web application. This applies to Django too. Controllers of this framework allow regular expression matching on URLs, to call the appropriate functions and field incoming requests. It assures sensible default requests and responses, with the ability to override. Tornado is the leanest and meanest among the featured Python Web frameworks. As I mentioned before, Tornado is fast, and is small too—in the sense that it is simple and yet efficient. These features make access points easy for extending the framework itself and directly managing the basic expectations of a Model View Controller framework. It also manages the basic operations of your website, such as parameter parsing, HTTP handing, etc. URLs of both Django and Tornado are almost similar. The only difference is that they use separate classes for each request. This makes it possible to create a class hierarchy for your requests, and then you can paste together a series of pages into a single parent class.

If you want to start a company or business that involves collecting data from users and storing it in a database, Django will really help you out. It has a form generator, which will allow you to define patterns for the database, and it also helps in generating HTML for an application which validates data on submission. The sophisticated data model reads your existing database schema, or creates one for you, and generates code to define the schema as software objects. In Tornado, the database connectivity is not built in by default, but it does not exist in a separate package and is known by the name torndb. This database helps in connecting to MySQL and issues your queries. For example, if you want to create your own environment for managing connections and retrying failed queries, then this is the one place you make real use of Tornado. It will work exactly how you want it to.
Django provides the ‘View’ part of MVC by making use of its template system efficiently.

The template system syntax of Tornado is a lot like Django’s and almost interchangeable: you get template inheritance, variable binding, looping and localisation support just as with Django. Yet, it executes a lot faster with less code under the hood. You can also short-circuit template compilation for speed by carefully instrumenting which files you’ll include conditionally. The template values are replaced in Django when you assign variables to an HTML template. This is also possible for the programs written outside of the Python code. This is fairly standard practice these days and Django does a great job of it. The template inheritance is a simple yet powerful way of defining standard headers and footers, as well as other features, so that they exist on every page. The templating is not the fastest on the Web, however; so you should plan for additional processing time.

Since it is such a mature system, Django has all kinds of other features as well: user authentication, localisation, Unicode handling, and the list goes on. If you want a framework that will cover everything you can possibly need and you just don’t have the time or skill to do a lot of the work to get it done, then Django is a great system for you. An interesting fact about it is the way it’s described—Django is the Web framework for perfectionists with deadlines.

The real differentiating glory of Tornado is the IOLoop, an asynchronous software library that integrates directly with kernel EPoll, KQueue and similar facilities. Also known as an Event Loop, this style of programming allows your program to detach its flow control, so the system can respond to another Web request while you wait for the kernel to dispatch your event. Tornado gives you great access to the internals of the system to make it do exactly what you want. The localisation and Unicode support is pretty thorough – you can create CSV key/value pair files, and look up the values based on the browser language headers. As long as you design your system in an open manner, you can serve your whole website in multiple languages and ensure it executes faster.

I do hope you’ve got a basic understanding of Python Web frameworks and familiarised yourself with Tornado. Now it’s your turn to dive into the code and come up with amazing Web applications. If you’re starting a new project, I highly recommend Python. It’s a great language with enough clarity, speed and features for you to bring together a global team to build a high-performance website. If you want to choose a Web framework that suits your team and project, the Python community members are there to help you out! They are really awesome!

References
[1] https://devcharm.com/articles/16/tornado-examples/
[2] http://carlosgabaldon.com
[3] http://backchannel.org
[4] https://wiki.python.org/moin/WebFrameworks

LEAVE A REPLY

Please enter your comment!
Please enter your name here