How to Understand Tornado?

Source: Internet
Author: User
Tags fast web
What is tornado? If it is a webserver, how does the backend work with Django and can it work with Node. js or PHP? If nginx is used as a web framework, what does it mean for her? Will the asynchronous IO Design of Tornado be affected in actual use? What is the difference between Tornado and node. js implementation and the design method )? ------------------------- What is tornado?
  1. If it is a webserver, how does the backend work with Django and can it work with Node. js or PHP?
  2. If nginx is used as a web framework, what does it mean for her? Will the asynchronous IO Design of Tornado be affected in actual use?
  3. What is the difference between Tornado and node. js implementation and the design method )?
-----------------------------

Note: The following is an understanding of the official instructions and Google:
  1. It can be used as a bare webserver, that is, its backend can run Django and web. py.
  2. It is a simple Python framework. The url is based on regular expressions and has its own template implementation, but there is no orm. This part looks similar to web. py.
  3. Non-blocking asynchronous IO, similar to node. js.
  4. Its front-end can be Nginx, which is similar to Nginx and Apache in Tomcat.

That is to say, it is simpler than Django as a web framework, and supports asynchronous IO, and does not need front-end webserver?
I am confused. Is Tornado a combination of Nginx, Django, and Node. js? Or Nginx * 20% + Django * 40% + Node. js * 40%? Reply content: I will answer the following questions:

> That is to say, it is simpler than Django as a web framework and supports asynchronous IO, and does not require front-end webserver?
> I am confused. Is Tornado a combination of Nginx, Django, and Node. js? Or Nginx * 20% + Django * 40% + Node. js * 40%?


You need to understand several concepts:
1. The web server is a listening port responsible for underlying processing such as HTTP link management, data sending and receiving, and HTTP protocol implementation.
2. The Web framework defines the process for processing a single HTTP request.
3. nginx is a reverse proxy server and a special web server application. It is not similar to web server.

Tornado is both a web server and a web framework.

For example, after you write a tornado application, you can directly run the tornado port on port 8000. At this time, you can access your Webpage Through localhost: 8000/foo. In two steps, tornado completes the listening and data acceptance of the I/O events at the bottom. This is tornado's mission as a web server. Then you write a get function in the corresponding place according to the process defined by the tornado framework to implement the specific content of this page. This is what tornado serves as a web framework.

So what is the use of nginx?

He is a reverse proxy. As the name suggests, the reverse proxy is used to forward received HTTP requests to other backend servers according to certain rules.

For example, you have run three tornado applications on one of your machines: foo1, foo2, and foo3. The ports are 8003, and respectively. You want users to directly access these applications through port 80. At this time, you can use nginx to achieve this goal. Let nginx run on port 80. When receiving the request, if it is/foo1, it will be forwarded to port 8000 for processing; if it is/foo2, it will be forwarded to port 8001 for processing, similar to foo3.


Therefore, tornado and nginx have no connection. In fact, many frameworks have implemented some simple web servers for debugging. Tornado web server is asynchronous and known for its ability to process a large number of inactive persistent connections. Therefore, its web server is a feature of feature. During the introduction, it will mention that it is a cool webserver.

Back to the topic, you can install nginx on your machine and write hello world with tornado.

Tornado is both a web server and a web framework. As a web server, it adopts the asynchronous IO network model, which is a very efficient model.

Web framework definition (http://wiki.python.org/moin/WebFrameworks )
A Web framework is a collection of packages or modules which allow developers to write Web applications or services without having to handle such low-level details as protocols, sockets or process/thread management.
As a web server, Tornado provides web framework APIs to directly build your own web programs. Tornado also supports WSGI (http://www.python.org/dev/peps/pep-0333/ ), That is, it can be used together with other python frameworks, such as django, bottle, and flask. Let's take a look at the performance evaluation of bottle in different servers, among which there is tornado (http://bottlepy.org/page/2009-12-19_Comparing_HelloWorld_Performance ). Additionally, the WSGI framework does not support Asynchronization. Therefore, Tornado is also one of the options if there is a logic web program for asynchronous calls.

I have never used Node. js, but the official website says:
Node. js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications.
Node. js can be understood as a javascript runtime environment or toolkit based on V8. It is an abstraction at the underlying layer and extends the ability of javascript to write server programs. So there will be different web frameworks based on it. From this perspective, Node. js and Tornado are not at the same level.
However, the same is the network model of single-process, single-thread asynchronous IO used by Node. js and Tornado. Both of them can write asynchronous non-blocking programs, but I think Node. js adopts a more thorough approach to asynchronous callback non-blocking style than Tornado due to the language characteristics of javascript. However, when there are multiple processes, short-term blocking of a process is acceptable.

Nginx is used because some single-process services cannot use multi-core CPUs, but also have single-host restrictions. Therefore, multiple process instances are usually started on multiple servers, use Nginx as a reverse proxy server on the front end to distribute web requests and load balancing. Nginx is written in C, with better concurrency and configurability, and better support for static files. Of course, this is a common scenario of Nginx, in fact, you can directly use Nginx to build web applications, you can refer to the OpenResty Project (http://openresty.org/ ).

In fact, the network model, web server, and web framework are three different layers. They do not conflict with each other and are selected. Understanding, may wish to clarify the concept first :) I suggest the landlord to read this paper C10K problem, although a little old http://www.kegel.com/c10k.html

In fact, you can also say that nginx is a framework, but it is for C.

The bare WebServer indicates that it supports the HTTP protocol. Since it supports the HTTP protocol, it doesn't matter what you put in front. nginx apache squid varnish. You can also use the wsgi module, but use a special communication protocol.

Why put nginx in front of tornado, because, although Python has multithreading, but Python interpreter has GIL http://en.wikipedia.org/wiki/Global_Interpreter_Lock This affects the use of multi-core capabilities by Python and Tornado, so multi-core capabilities can only be used through multi-process. Since there are multiple processes, nginx haproxy needs to be placed at the front end as the reverse proxy for Server Load balancer, or the wsgi module of these application servers is used to manage the process lifecycle. In addition, static file services are also more advantageous for nginx and the like. (They directly forward the request to the tornado process. You don't have to worry about how the request comes, so concentrate on writing your program .)

The frameworks of many other scripting languages have similar problems, such as ruby, while Nodejs does not have any connection.

The core of Nodejs is that it uses the event/callback mechanism that JavaScript is inherently good at to implement Asynchronization.
Python does not support anonymous functions and closures well. Therefore, Tornado uses classes and callback methods to implement Asynchronization. There is no concept of events here. (I have not studied Tornado in depth, so I know about it)
In addition, Tornado encapsulates many common functions such as static file processing, Router, and templates. Nodejs is basically a pure Protocol framework, and these functions need to be written by themselves. Nodejs + Express is similar to Tornado.

If you want to implement a middleware or intermediate layer and perform some advanced protocol processing, nodejs may be more competent.
In tornado, it is okay to call the services developed with php or nodejs. It mainly depends on the architecture design. Tornado is an asynchronous http framework. fb was first used for feed stream notifications using tornado. tornado has a built-in asynchttpclient, which is especially suitable for websites based on rest web Services and @ gen. coroutine and yield can basically convert asynchronous code into synchronous code. Tornado has a small amount of code. It is especially suitable for those who are interested to study and use it on their own.

However, tornado is not a small problem. For websites, the most important thing is that tornado is single-threaded (although multiple processes can be implemented through fork), while the web backend is mostly rdbms, in particular, the main driver of mysql is synchronous blocking, that is to say, access to the database will block tornado, so there is a large limit on Web pages. They are all long .. In fact, it is better to understand epoll and understand ioloop in tornado. First of all, I think the two functions of web server and web framework are not in conflict, because the function of http server is connection access, business logic processing, return to the page. Therefore, tornado, node. js, django, and nginx can all implement such functions, but they are classified as web server or web framework due to different application scenarios.
First, let's talk about nginx. The main application scenario is reverse proxy, which is responsible for frontend connection access and then requests back-end services. Nginx provides some load balancing functions to enhance the load capacity of web Services. At the same time, nginx can also be used as a web framework, but its C language development costs are large, so there are few users. However, openresty developed by @ agentzh can use Lua to write nginx extensions, which also enhances nginx's ability to process business logic as a web framework.
Node. the positioning of js and node is not completely the web server and web framework. It implements a set of javascript Runtime on the server, but due to its own Asynchronous Network characteristics, therefore, as a network service framework is very suitable, we can see that the HTTP server implemented by node is actually very simple, which also gave birth to the goal of express is the web framework middleware.
I only have a rough understanding of django. I feel that it is positioned as a full set of solutions for web services (such as rails). It provides common functions for web services such as orm and abstract background management, therefore, it is positioned as a web framework, but because it is synchronized (I haven't paid much attention to it for many years, but I don't know it now), it affects access capabilities, therefore, an asynchronous reverse proxy is usually required.
Tornado is more like a lightweight web framework that provides asynchronous capabilities. However, it is easier to support business logic processing than django, so it is more suitable for service middleware like node.
From the perspective of business logic processing, although the asynchronous processing capability has been greatly improved, node and tornado are both single processes, so it is generally necessary to start multiple instances, the front end uses nginx for access and load balancing.
Therefore, from the perspective of architecture selection, nginx implements front-end access, and the selection of service logic is based on requirements. If it is just the most basic web service, such as cms, django is more convenient, this saves a lot of development effort. Node and tornado are suitable for implementing the middleware of complex web systems ...... The answer is a bit one-sided. I pay more attention to the web framework. In fact, tornado is far more than that.
1. High-Performance Network Library, which can be used with gevent, twisted, and libevent. Provides asynchronous io support and time-out event processing. On this basis, tcpserver and httpclient, especially curlhttpclient, are provided to be the first in the existing http client. It can be used for crawlers and game servers. As far as I know, tornado has been used as a game server in the industry.

2. web framework, which can be matched with django and flask. Provides required web framework components such as routing and templates. The other difference is that tornado is asynchronous and is naturally suitable for long round training. This is why friendfeed invented tornado,
Currently, flask is supported, but gevent and so on must be used.

3. relatively complete http servers, which can be compared with nginx and apache, but only support http1.0. Therefore, using nginx not only aims to make better use of multiple cores, but also enables it to support http1.1.

4. Complete wsgi servers, which can be compared with gunicore and gevent wsgi servers. That is to say, flask can be run on tornado and tornado can be accelerated to flask.
5. provides complete websocket support, which facilitates html5 games. Like zhihu, longpolling uses websocket, but the websocket mobile phone does not support very well. Some time ago, I had to use timed ajax to send a large number of requests. I hope the mobile browser will catch up quickly.

Tornado, you deserve tornado's built-in web server, which has good performance, but it is recommended to run behind nginx because it simplifies many functions.

Tornado itself is a web framework, but it does not implement many functions in advance and only retains necessary components. This is a somewhat different idea from Django.
Although the idea is the opposite, Django actually focuses on a well-designed core. If this core is extracted separately, it is similar to Tornado's scale and functions. Its rich functionality (or even complexity) is just an embodiment of the "one-stop framework" concept.
To put it simply, they are simple frameworks, and one is to assign all the components you may need to your framework by default. Which one you like to use to view your hobbies. The trouble with the former is that you have to implement it on your own when you encounter some problems. The trouble with the latter is that if it is preset to you, you must implement it on your own if it cannot meet your requirements. In the final analysis, it is to find a balance point, try to facilitate development.

In asynchronous mode, I personally do not recommend node. js. js is rarely used on the server. Many components are incomplete and it takes a long time to mature. Python can be used by many existing components.

Finally, if you are concerned about performance, try pypy. in Linux, the network I/O is not inferior to node. js. However, in most cases, performance problems are not written in Python, but in the database. 1. Lightweight frameworks. Starting from web. py, flask and bottle are all lightweight frameworks, while tornado is representative of lightweight frameworks.
2. The high-performance asynchronous framework writes things directly based on ioloop, And you think tornado is not just a web framework.
3. For the queue server, we implement a two-level tornado server model. The previous model is responsible for web allocation and concurrency, and the latter is responsible for single-thread high-cpu computing, which easily solves the block problem of high computing workload of a single thread.

The WSGI function is negligible.

After understanding the features of tornado, we found that the only thing that needs to work with tornado is nginx, which can be used for load balancing and blocking static file requests. In addition, you need a mysql instance and try torndb.

Tornado can greatly reduce the number of components used in the web system, and implement your system with the minimum amount of code and extremely streamlined architecture. Tornado is a web server. Django is a fast web programming framework. Node. js is a programming environment. You can directly use Tornado to write a website, but it only has the most basic functions. You do not need to write a website, while DJANGO can help you quickly write website programs, then it is deployed on Tornado or other web servers. Node. js is a programming environment similar to PYTHON, which can implement many things. I think the real reality for a website is PHP, rails, and django. It is really important to quickly implement your ideas. Performance and other things are not important at first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.