- A Beginner’s Guide to Python Tornado
- What is Synchronous Program?
- What is Asynchronous Program?
- Asynchronous vs Synchronous Web Framework:
- Node.js vs Tornado
- A Simple ‘Hello World’ Example
- Complex Example of Tornado Async
- WebSockets Using Tornado:
- Conclusion
- ¶
- Quick links¶
- Hello, world¶
- Installation¶
- Documentation¶
- Discussion and support¶
A Beginner’s Guide to Python Tornado
The web is a big place now. We need to support thousands of clients at a time, and here comes Tornado. Tornado is a Python web framework and asynchronous network library, originally developed at FriendFreed.
Tornado uses non-blocking network-io. Due to this, it can handle thousands of active server connections. It is a saviour for applications where long polling and a large number of active connections are maintained.
Tornado is not like most Python frameworks. It’s not based on WSGI, while it supports some features of WSGI using module `tornado.wsgi`. It uses an event loop design that makes Tornado request execution faster.
What is Synchronous Program?
A function blocks, performs its computation, and returns, once done . A function may block for many reasons: network I/O, disk I/O, mutexes, etc.
Application performance depends on how efficiently application uses CPU cycles, that’s why blocking statements/calls must be taken seriously. Consider password hashing functions like bcrypt, which by design use hundreds of milliseconds of CPU time, far more than a typical network or disk access. As the CPU is not idle, there is no need to go for asynchronous functions.
A function can be blocking in one, and non-blocking in others. In the context of Tornado, we generally consider blocking due to network I/O and disk, although all kinds of blocking need to be minimized.
What is Asynchronous Program?
1) Single-threaded architecture:
Means, it can’t do computation-centric tasks parallely.
2) I/O concurrency:
It can handover IO tasks to the operating system and continue to the next task to achieve parallelism.
3) epoll/ kqueue:
Underline system-related construct that allows an application to get events on a file descriptor or I/O specific tasks.
4) Event loop:
It uses epoll or kqueue to check if any event has happened, and executes callback that is waiting for those network events.
Asynchronous vs Synchronous Web Framework:
In case of synchronous model, each request or task is transferred to thread or routing, and as it finishes, the result is handed over to the caller. Here, managing things are easy, but creating new threads is too much overhead.
On the other hand, in Asynchronous framework, like Node.js, there is a single-threaded model, so very less overhead, but it has complexity.
Let’s imagine thousands of requests coming through and a server uses event loop and callback. Now, until request gets processed, it has to efficiently store and manage the state of that request to map callback result to the actual client.
Node.js vs Tornado
Most of these comparison points are tied to actual programming language and not the framework:
- Node.js has one big advantage that all of its libraries are Async. In Python, there are lots of available packages, but very few of them are asynchronous
- As Node.js is JavaScript runtime, and we can use JS for both front and back-end, developers can keep only one codebase and share the same utility library
- Google’s V8 engine makes Node.js faster than Tornado. But a lot of Python libraries are written in C and can be faster alternatives.
A Simple ‘Hello World’ Example
Note: This example does not use any asynchronous feature.
Using AsyncHTTPClient module, we can do REST call asynchronously.
As you can see `yield http_client.fetch(url)` will run as a coroutine.
Complex Example of Tornado Async
WebSockets Using Tornado:
Tornado has built-in package for WebSockets that can be easily used with coroutines to achieve concurrency, here is one example:
One can use a WebSocket client application to connect to the server, message can be any integer. After processing, the client receives the result if the integer is prime or not.
Here is one more example of actual async features of Tornado. Many will find it similar to Golang’s Goroutine and channels.
In this example, we can start worker(s) and they will listen to the ‘tornado.queue‘. This queue is asynchronous and very similar to the asyncio package.
Conclusion
1) Asynchronous frameworks are not much of use when most of the computations are CPU centric and not I/O.
2) Due to a single thread per core model and event loop, it can manage thousands of active client connections.
3) Many say Django is too big, Flask is too small, and Tornado is just right:)
¶
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.
Quick links¶
Hello, world¶
Here is a simple “Hello, world” example web app for Tornado:
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start()
This example does not use any of Tornado’s asynchronous features; for that see this simple chat room.
Installation¶
Automatic installation:
Tornado is listed in PyPI and can be installed with pip or easy_install . Note that the source distribution includes demo applications that are not present when Tornado is installed in this way, so you may wish to download a copy of the source tarball as well.
Manual installation: Download tornado-4.5.dev1.tar.gz:
tar xvzf tornado-4.5.dev1.tar.gz cd tornado-4.5.dev1 python setup.py build sudo python setup.py install
The Tornado source code is hosted on GitHub.
Prerequisites: Tornado 4.3 runs on Python 2.7, and 3.3+ For Python 2, version 2.7.9 or newer is strongly recommended for the improved SSL support. In addition to the requirements which will be installed automatically by pip or setup.py install , the following optional packages may be useful:
- concurrent.futures is the recommended thread pool for use with Tornado and enables the use of ThreadedResolver . It is needed only on Python 2; Python 3 includes this package in the standard library.
- pycurl is used by the optional tornado.curl_httpclient . Libcurl version 7.19.3.1 or higher is required; version 7.21.1 or higher is recommended.
- Twisted may be used with the classes in tornado.platform.twisted .
- pycares is an alternative non-blocking DNS resolver that can be used when threads are not appropriate.
- monotonic or Monotime add support for a monotonic clock, which improves reliability in environments where clock adjustements are frequent. No longer needed in Python 3.3.
Platforms: Tornado should run on any Unix-like platform, although for the best performance and scalability only Linux (with epoll ) and BSD (with kqueue ) are recommended for production deployment (even though Mac OS X is derived from BSD and supports kqueue, its networking performance is generally poor so it is recommended only for development use). Tornado will also run on Windows, although this configuration is not officially supported and is recommended only for development use.
Documentation¶
This documentation is also available in PDF and Epub formats.
- User’s guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example — a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream -based TCP server
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- What’s new in Tornado 4.4.1
- What’s new in Tornado 4.4
- What’s new in Tornado 4.3
- What’s new in Tornado 4.2.1
- What’s new in Tornado 4.2
- What’s new in Tornado 4.1
- What’s new in Tornado 4.0.2
- What’s new in Tornado 4.0.1
- What’s new in Tornado 4.0
- What’s new in Tornado 3.2.2
- What’s new in Tornado 3.2.1
- What’s new in Tornado 3.2
- What’s new in Tornado 3.1.1
- What’s new in Tornado 3.1
- What’s new in Tornado 3.0.2
- What’s new in Tornado 3.0.1
- What’s new in Tornado 3.0
- What’s new in Tornado 2.4.1
- What’s new in Tornado 2.4
- What’s new in Tornado 2.3
- What’s new in Tornado 2.2.1
- What’s new in Tornado 2.2
- What’s new in Tornado 2.1.1
- What’s new in Tornado 2.1
- What’s new in Tornado 2.0
- What’s new in Tornado 1.2.1
- What’s new in Tornado 1.2
- What’s new in Tornado 1.1.1
- What’s new in Tornado 1.1
- What’s new in Tornado 1.0.1
- What’s new in Tornado 1.0
Discussion and support¶
You can discuss Tornado on the Tornado developer mailing list, and report bugs on the GitHub issue tracker. Links to additional resources can be found on the Tornado wiki. New releases are announced on the announcements mailing list.
Tornado is available under the Apache License, Version 2.0.
This web site and all documentation is licensed under Creative Commons 3.0.
© Copyright 2009-2016, The Tornado Authors. Revision 4ecc31a5 .
Versions latest Downloads On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.