Because I changed my job, the previous game engine was temporarily put down, but it won't stop. This project will be completed in my spare time.
--------------------------------------- The boring split line. The following is the body -------------------------------------------
Tornado my previousArticleHas been introduced many times, so I will not detail it here,
For details, see:
Game snake-use Tornado to build one of the high-performance Web Applications
Snake games-using Tornado to build a high-performance Web 2-autoreload
Because the official website is walled and discussion groups are also walled, there is very little tornado information and the official website information is not detailed. Therefore, many children's shoes have no idea how to deploy and use tornado. The main purpose of this article is to teach beginners how to use tornado in the production environment.
Tornado is an asynchronous Web framework and server, so it is very suitable for developing longpulling chat and other applications. However, Tornado is also a high-performance HTTP server and can also be used as a wsgiserver. So even if your website does not use the tornado framework, but uses Web. py or Django for Development (Long live), Tornado can still be used to accelerate your website. Using tornado instead of FastCGI can greatly improve the performance, and the concurrency capability that can be carried has been multiplied (you can profile yourself, this article will only introduce if you do ).
Next we will introduce how to configure. Here, we assume that one of your websites written with Django is happy on a Linux Server (Ubuntu or centos, but I have never tried to get involved in other releases, windows? You are joking.) as your website becomes increasingly popular, you will feel overwhelmed by servers. At this time, Tornado appeared, and he could let you survive for a few months, saving money to bring your sister ...... back to the subject. According to the recommended deployment method on the official website, we still adopt the deployment method of nginx to reverse proxy to N tornado server instances through upstream. So
Setp1: Install supervisord
Tornado does not provide its own daemon capabilities, so we need to use a service management tool to manage tornado processes. supervisord is a very practical process management tool implemented using python. You can easily manage N processes and support process groups. Supervisord can be installed through sudo easy_install supervisor. Of course, you can also download it from the supervisord official website and install setup. py install.
Step 2: Add a tornado Server File (such as Serv. py) to the Django site)
Create a file Serv. py in the root directory of the Django site. The content is as follows:
1 _ Author __ = ' Alexander '
2 Import OS
3 Import Sys
4
5 From Tornado. Options Import Options, define, parse_command_line
6 Import Django. Core. Handlers. wsgi
7 Import Tornado. httpserver
8 Import Tornado. ioloop
9 Import Tornado. Web
10 Import Tornado. wsgi
11
12 _ Here = OS. Path. dirname (OS. Path. abspath ( _ File __ ))
13 SYS. Path. append (_ here)
14 SYS. Path. append (OS. Path. Join (_ here, ' .. ' ))
15 SYS. Path. append (OS. Path. Join (_ here, ' ../Contrib ' ))
16 OS. Environ [ ' Django_settings_module ' ] = " Settings "
17
18 Def Main (port ):
19 Wsgi_app = Tornado. wsgi. wsgicontainer (
20 Django. Core. Handlers. wsgi. wsgihandler ())
21 Tornado_app = Tornado. Web. Application (
22 [( ' .* ' , Tornado. Web. fallbackhandler, dict (fallback = Wsgi_app )),
23 ])
24 Server = Tornado. httpserver. httpserver (tornado_app)
25 Server. Listen (port)
26 Tornado. ioloop. ioloop. instance (). Start ()
27
28 If _ Name __ = ' _ Main __ ' :
29 Main (INT (SYS. argv [ 1 ])
Here, we use the first parameter to specify the tornado service listening port. This is more flexible and will be used in subsequent steps. At this time, we can use
Python serv-py 8000
This command is used to start the server.
Step 3: Configure supervisord
The supervisord installed in step 1 has not been configured, so we need to create a configuration file template first. Run
Echo_supervisord_conf > /Etc/supervisord. conf
At this time, a configuration file is created in/etc/. Use Vim to open the file and add the following section to the configuration file's ass.
[ Program: Web ]
Command = Python/var/www/site/Serv. py 80 % (Process_num) 02d
Process_name = % (Program_name) S _ % (process_num) 02d
Umask = 022
Startsecs = 0
Stopwaitsecs = 0
Redirect_stderr = True
Stdout_logfile = /Tmp/codoon. Log
Numprocs = 4
Numprocs_start = 1
This configuration will start four tornado service processes to listen to the four ports 8001,8002, 8003,8004, respectively.
Command is the command to be executed. Here, the service process of Tornado is started using the python/var/www/site/Serv. py port number.80% (Process_num) 02d uses process numbers to generate port numbers. The following process_name parameter is also used. The file name to be specified here is the Serv. py file we created in the previous step.
Process_name is the name of a process. Because four processes need to be started here, process_num is used to distinguish them.
Umask isProgramExecution permission Parameters
Startsecs is the waiting time for the program to start.
Stopwaitsecs this parameter is the waiting time for the program to stop
Redirect_stderr: this parameter redirects the error stream to the stream output of STD, which saves the configuration of a log file. Of course, this parameter can also be used to separate the configuration of log files.
The stdout_logfile parameter is the path of the log file output by the STD stream. Tornado will output all the request and error information, which can be used for log processing and separation, in the program, you only need to print the data to the STD stream.
Numprocs this parameter specifies the number of processes. Here is 4, indicating to start four tornado Processes
Numprocs_start specifies the start Number of the Process number. Here, it is 1, so that the % (process_num) 02d part of the preceding command and process_name will be replaced with 01 ~ 05 string
After the configuration is modified, WQ is saved and exited. Execute:
Supervisorctl reload
After the configuration is reloaded, these processes are started.
Step 4: Modify the nginx Configuration
First, find your site configuration file in the vhost directory. After opening it, add upstream content to the header.
Upstream frontends {
Server 127.0.0.1: 8001;
Server 127.0.0.1: 8002;
Server 127.0.0.1: 8003;
Server 127.0.0.1: 8004;
}
In the server configuration section, find
Location/{This configuration section
Previously, FastCGI was used, so the configuration in it may be like this.
# Host and port to FastCGI Server
# Fastcgi_pass 127.0.0.1: 8081;
# Fastcgi_param path_info $ fastcgi_script_name;
# Fastcgi_param request_method $ request_method;
# Fastcgi_param QUERY_STRING $ QUERY_STRING;
# Fastcgi_param content_type $ content_type;
# Fastcgi_param content_length $ content_length;
# Fastcgi_pass_header authorization;
# Fastcgi_param remote_addr $ remote_addr;
# Fastcgi_param server_protocol $ server_protocol;
# Fastcgi_param server_port $ server_port;
# Fastcgi_param SERVER_NAME $ SERVER_NAME;
# Fastcgi_intercept_errors off;
Delete all these items and change them to this
Location /{
}
Add the upstream configuration in {} as follows:
Location /{
Proxy_pass_header server;
Proxy_set_header host $ http_host;
Proxy_set_header X-real-IP $ remote_addr;
Proxy_set_header X-scheme $ scheme;
Proxy_pass http: // frontends;
Proxy_next_upstream error;
}
Save the configuration file and run the nginx restart command nginx-s reload (note that the nginx file is located differently in different releases)
Then you can see your website through the domain name, and try to see if it is much faster
note: the number of Tornado processes enabled in the production system is better. This is wise. According to my stress test results, the number of CPU cores x 2 is the best, no improvement is wasted. (why do we get 2? Because there is a kind of CPU technology called hyper-threading ). 4 processes on my VPs. If the 8-core intelcpu needs to fully tap the CPU potential, it needs to open 16 processes