Tornado - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki

Group:

right light team

Guide

Below are quick links for different part of this page:

  • Introduction
  • Components
  • The design model of Tornado
  • The network model of Tornado
  • More about Tornado
  • Pros and Cons
  • Templates and UI
  • Code Example
  • Conclusion
  • Reference
  • Contribution

Introduction

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.

Alt text[2]

Components

Tornado can be roughly divided into four major components:

  • A web framework (including RequestHandler which is subclassed to create web applications, and various supporting classes).

  • Client- and server-side implementions of HTTP (HTTPServer and AsyncHTTPClient).

  • An asynchronous networking library including the classes IOLoop and IOStream, which serve as the building blocks for the HTTP components and can also be used to implement other protocols.

  • A coroutine library (tornado.gen) which allows asynchronous code to be written in a more straightforward way than chaining callbacks. This is similar to the native coroutine feature introduced in Python 3.5 (async def). Native coroutines are recommended in place of the tornado.gen module when available.

The Tornado web framework and HTTP server together offer a full-stack alternative to WSGI. While it is possible to use the Tornado HTTP server as a container for other WSGI frameworks (WSGIContainer), this combination has limitations and to take full advantage of Tornado you will need to use Tornado’s web framework and HTTP server together.

The design model of Tornado

Tornado is not only a web framework, but also a complete implementation of HTTP server and client. On this basis, it provides Web services, which can be divided into four layers:

  1. Web framework: the top layer, including the functions required by the web framework such as processor, template, database connection, authentication, localization, etc.
  2. Http / HTTPS layer: implement HTTP server and client based on HTTP protocol
  3. TCP layer: implement TCP server responsible for data transmission
  4. Event layer: bottom layer, handling IO events Alt text

The network model of Tornado

Tornado adopts the mode of "multi process + non blocking + epoll" Alt text

More about Tornado

  • Tornado is a framework for writing responses to HTTP requests.

  • Tornado itself provides a lightweight, fast and flexible template language in tornado.template Module.

  • Torndb used by tornado is a lightweight module based on MySQL DB encapsulation[4]

  • Tornado web server has a lot of security considerations since its design, which makes it easier to prevent those common vulnerabilities. Secure cookies prevent users' local state from being secretly modified by malicious code in their browser.

  • Tornado is an open source version of the extensible non blocking web server and its related tools used by friendreed. In order to make full use of non blocking server environment, this framework also includes some useful tools and optimizations.

  • Tornado is quite different from the current mainstream web server framework (including most Python frameworks): it is a non blocking server, and it is quite fast.

  • Thanks to its non blocking and epoll application, tornado can handle thousands of connections per second, which means that tornado is an ideal web framework for real-time web services.[5]

Pros

Tornado is suitable for highly customized websites with large number of visits and many asynchronous situations

  • Less and better (Lightweight Framework)

  • Pay attention to superior performance and fast speed

  • Solve high concurrency (request processing is a non blocking call based on callback)

  • Asynchronous non blocking

  • WebSockets long connection

  • Embedded HTTP server

  • A single thread asynchronous network program, which runs multiple instances according to the number of CPUs when it is started by default (taking advantage of the multi-core CPU)

  • Modules can be customized[3]

Cons

  • There are many third-party modules for template and database, which is not conducive to encapsulation as a functional module

Templates and UI

Tornado includes a simple, fast, and flexible templating language. This section describes that language as well as related issues such as internationalization.

Tornado can also be used with any other Python template language, although there is no provision for integrating these systems into RequestHandler.render. Simply render the template to a string and pass it to RequestHandler.write

Code example

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()



Conclusion

Tornado framework is a more robust and lightweight Python web framework than other web frameworks in dealing with server performance problems. Tornado is a customizable non blocking asynchronous loading framework, which is quite fast.

In general, tornado is a lightweight framework with high performance and high speed. Tornado is worth learning.

Reference

  1. Tornado -JunChow520 [online] Available at: https://www.jianshu.com/p/3a928ade93dc/.
  2. Tornado Web Server. [online] Available at: https://www.tornadoweb.org/en/latest/.
  3. The advantages and disadvantages of Tornado [online] Available at: https://www.cnblogs.com/zhumengke/articles/11297303.html/.
  4. On tornado framework [online] Available at: https://www.jianshu.com/p/3756a422883a/.
  5. Basic knowledge of tornado [online] Available at: https://www.cnblogs.com/yangmingxianshen/p/8983561.html/.

Contribution

  • Ao Gao (@LeuE) : Part-- 'Introduction', 'Components', 'Code examples','Templates and UI' and 'Reference'.

  • Jiarui Song (@BY-sjr) : Part-- 'The design model of Tornado', 'The network model of Tornado', 'More about Tornado', 'Pros and Cons' and 'Conclusion'.