background preloader

Tornado

Facebook Twitter

Rarely Mentioned Benefits of Tornado Framework. What is Tornado?

Rarely Mentioned Benefits of Tornado Framework

Tornado is a popular event-loop based web framework. Most people who talk about Tornado usually mentioned how fast it is when hitting “Hello World” using Apache Benchmark. This post is not about that. Background jobs In most web application request cycle, it’s common for the request handler (or controller) to perform various tasks that are less important than page delivery (and sometimes expensive). Django applications solve this problem by creating jobs on Celery or Gearman. Rails applications solve this problem by creating jobs on Resque or various AMQP solutions. Tornado applications solve this problem through IOLoop.add_callback() within itself, without any external daemons: Python - Any examples of tornado.process implementation. Sockjs-protocol-0.3.3.py. The SockJS server provides one or more SockJS services.

sockjs-protocol-0.3.3.py

The services are usually exposed with a simple url prefixes, like: or . Llegaard - SockJS and Tornado for Python real-time web projects. At Djangocon EU last year, I gave a talk about how you could easily get up and running with real-time web applications using Django and Socket.IO.

llegaard - SockJS and Tornado for Python real-time web projects

I have now been running an application using this principle for a year and I am everything but a fan of it. I decided i had to switch to something else. My first impression of SockJS wasn't very good. I thought the documentation was poor and the commit on the main (client) library wasn't very new. However, after reviewing other possibilites I decided that SockJS with sockjs-tornado had to be the best option at the moment. I found a great gist that showed most of the way. By now, you have probably realized that we are storing clients on the class. A. Jesse Jiryu Davis. Async frameworks like Tornado scramble our usual unittest strategies: how can you validate the outcome when you do not know when to expect it?

A. Jesse Jiryu Davis

Tornado ships with a tornado.testing module that provides two solutions: the wait / stop pattern, and gen_test. To begin, let us say we are writing an async application with feature like Gmail's undo send: when I click "send", Gmail delays a few seconds before actually sending the email. It is a funny phenomenon, that during the seconds after clicking "sending" I experience a special clarity about my email. It was too angry, or I forgot an attachment, most often both.

If I click the "undo" button in time, the email reverts to a draft and I can tone it down, add the attachment, and send it again. To write an application with this feature, we will need an asynchronous "delay" function, and we must test it. When we run this, it prints: And if we replace delay(1) with delay(2) it fails as expected: Great! If we run this: Success! Django with SockJS — Idea of the day. 21 September 2012 Few days ago Peter Bengtsson wrote an interesting blog post on SockJS: Real-timify Django with SockJS The article is quite brief, let me try to provide step-by-step instructions on how to start your first Django on SockJS project.

Django with SockJS — Idea of the day

Python servers First, it's important to understand that there are many HTTP (okay, WSGI) servers for Django. Currently there are a number of SockJS libraries that work with variety of servers (with quality and completeness varying): Tornado nginx-sunu. Tornado with django. It took me a bit of time to get django working with the tornado web server.

Tornado with django

There are numerous tutorials out there, and there are a few frameworks posted on github, but nevertheless, none of them did exactly what I wanted. Basically, what I needed was a WebSocket server which also handled authentication. Knowing that there were numerous nice django apps for authentication (such as social_auth), I decided to try to reuse them.

Keep in mind, there are also ways to embed websocket frameworks inside of django, but they all seemed too heavyweight for what I was trying to do here. Dependency Injection with Tornado. One of the most powerful patterns I use is Dependency Injection.

Dependency Injection with Tornado

The main idea behind Dependency Injection is collaboration. Collaboration among objects for solving a problem. Do you remember divide and conquer algorithms? These algorithms works breaking down a big and complex problem into several sub-problems which become simpler and simpler and can be solved easily, then the solutions to the sub-problems must be combined to give a solution for the original problem. This is the goal of Dependency Injection, defining the way our objects are going to be combined for collaborating. Being truly asynchronous with Tornado. In the past few days I’ve had the chance to play a bit with Tornado, a non-blocking web server for Python similar to node.js.

Being truly asynchronous with Tornado

Tornado works by using something like epoll or whatever I/O event notification exists in the system. Tornado is single threaded and works like a fancy while (True) loop. As a consequence, whenever we do some blocking I/O (e.g. database call) the web server cannot process other requests. While that may not be a big deal on very low traffic sites or if your I/O subsystem is extremely fast, it starts becoming an issue as your traffic grows or if you’re hosted on something like Amazon AWS; notorious for its slow and inconsistent disk I/O performance. Gracefully handling application exceptions in a Tornado application. Tornado Asynchronous Handler. Python - Tornado URL query parameters.