Python Web framework Tornado running and deployment, pythontornado

Source: Internet
Author: User

Python Web framework Tornado running and deployment, pythontornado

This article provides an example of how to run and deploy the Python Web framework Tornado for your reference. The details are as follows:

I. Operation and deployment
Tornado has its own HTTPServer built in and runs and deploys it differently from other Python web frameworks. You need to write a main () function to start the service, instead of configuring a WSGI container to run your application:

def main():  app = make_app()  app.listen(8888)  IOLoop.current().start()if __name__ == '__main__':  main()

Configure your operating system or process manager to run this program to start the service. Note that it is necessary to increase the maximum number of file handles allowed by each process (to avoid the "Too allow open files" error ). To increase the upper limit (for example, set it to 50000), you can use the ulimit command to modify/etc/security/limits. conf or set minfds in your supervisord configuration.

2. Processes and ports
Because of Python GIL (Global interpreter lock), it is necessary to run multiple Python processes to make full use of multiple CPU machines. Generally, it is best to run a process on each CPU.

Tornado contains a built-in multi-process mode to start multiple processes at a time, which requires a slight change on the main function:

def main():  app = make_app()  server = tornado.httpserver.HTTPServer(app)  server.bind(8888)  server.start(0) # forks one process per cpu  IOLoop.current().start()

This is the easiest way to start multiple processes and share the same port with them, though it has some limitations. First, each sub-process will have its own IOLoop, so before fork, It is important (or even indirect) not to be exposed to global IOLoop instances ). Secondly, in this model, it is difficult to achieve zero-downtime update. Finally, it is more difficult for all processes to monitor the same ports independently.

For more complex deployment, it is recommended to start independent processes and have them listen to different ports. supervisord's process group function is a good way. When each process uses a different port, an external server Load balancer, such as HAProxy or nginx, usually needs to provide a single address to the visitor.

Iii. Running behind the Server Load balancer
When running in a server Load balancer such as nginx, it is recommended to pass xheaders = True to the HTTP server constructor. This tells Tornado to use an HTTP header like X-Real-IP to obtain the user's IP address rather than regard all traffic as the IP address of the Server Load balancer.

This is an original nginx configuration file, which is similar to the configuration we use in FriendFeed. It is assumed that nginx and Tornado server are running on the same machine, and four Tornado servers are running on port 8000-8003:

user nginx;worker_processes 1;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events {  worker_connections 1024;  use epoll;}http {  # Enumerate all the Tornado servers here  upstream frontends {    server 127.0.0.1:8000;    server 127.0.0.1:8001;    server 127.0.0.1:8002;    server 127.0.0.1:8003;  }  include /etc/nginx/mime.types;  default_type application/octet-stream;  access_log /var/log/nginx/access.log;  keepalive_timeout 65;  proxy_read_timeout 200;  sendfile on;  tcp_nopush on;  tcp_nodelay on;  gzip on;  gzip_min_length 1000;  gzip_proxied any;  gzip_types text/plain text/html text/css text/xml        application/x-javascript application/xml        application/atom+xml text/javascript;  # Only retry if there was a communication error, not a timeout  # on the Tornado server (to avoid propagating "queries of death"  # to all frontends)  proxy_next_upstream error;  server {    listen 80;    # Allow file uploads    client_max_body_size 50M;    location ^~ /static/ {      root /var/www;      if ($query_string) {        expires max;      }    }    location = /favicon.ico {      rewrite (.*) /static/favicon.ico;    }    location = /robots.txt {      rewrite (.*) /static/robots.txt;    }    location / {      proxy_pass_header Server;      proxy_set_header Host $http_host;      proxy_redirect off;      proxy_set_header X-Real-IP $remote_addr;      proxy_set_header X-Scheme $scheme;      proxy_pass http://frontends;    }  }}

Iv. static file and File Cache
In Tornado, you can specify a special static_path in the application to provide static file services:

settings = {  "static_path": os.path.join(os.path.dirname(__file__), "static"),  "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",  "login_url": "/login",  "xsrf_cookies": True,}application = tornado.web.Application([  (r"/", MainHandler),  (r"/login", LoginHandler),  (r"/(apple-touch-icon\.png)", tornado.web.StaticFileHandler,   dict(path=settings['static_path'])),], **settings)

These settings automatically submit all requests starting with/static/to the static directory, such as http: // localhost: 8888/static/foo.png provides the foo.png file through the specified static directory. We will also automatically provide/robots.txt and/favicon. ico from the static directory (although they do not start with/static/prefix ).

In the settings above, we explicitly configure Tornado to get the apple-touch-icon.png file from the StaticFileHandler root, although the file is in the static file directory. (The regular expression capture group must tell the StaticFileHandler request file name and call the capture group to pass the file name as a method parameter to the handler.) You can do the same thing, for example, provide sitemap from the root of the website. xml file. Of course, you can also avoid forging the apple-touch-icon.png of the root directory by using the <link/> tag in your HTML.

To improve performance, it is usually a good idea to allow the browser to actively cache static resources, in this way, the browser will not send unnecessary If-Modified-Since or Etag requests that may be blocked during page rendering. Tornado uses the static content version (static content versioning) to support this function.

To use these functions, use the static_url method in your template, instead of directly entering the URL of the static file in your HTML:

The static_url () function translates the relative path into a URI similar to/static/images/logo.png? V = aae54. the v parameter in the parameter is the hash of the logo.png content, and its existence causes the Tornado service to send a cache header to the user's browser, which will enable the browser to cache content indefinitely.

Because the parameter v is based on the file content, if you update a file and restart the service, it will send a new v value, so the user's browser will automatically pull the new file. If the file content does not change, the browser will continue to use the locally cached copy instead of checking for updates from the server, significantly improving the rendering performance.

In production, you may want to provide static files through a better static server, such as nginx, you can configure any web server to identify through static_url () provides the version label and sets the cache header accordingly. The following is part of the nginx configuration we use in FriendFeed:

location /static/ {  root /var/friendfeed/static;  if ($query_string) {    expires max;  } }

V. Debug mode and automatic reloading
If you pass the debug = True configuration to the Application constructor, the Application runs in debug/development mode. In this mode, some features will be enabled for convenience of Development (each can also be used as an independent label, if they are specified specifically, all of them will have their own priorities ):

1. autoreload = True:The application will observe whether its source file changes and reload itself when any file changes. This reduces the need to manually restart the service during development. However, in debug mode, some errors (such as syntax errors during import) may cause the service to be closed and cannot be automatically restored.
2. compiled_template_cache = False:The template will not be cached.
3. static_hash_cache = False:Static file hashing (used by the static_url function) will not be cached.
4. serve_traceback = True:If an exception is not captured in RequestHandler, an error page containing the call stack information is generated.
The autoreload mode is incompatible with the multi-process mode of HTTPServer. start transmits parameters other than 1 (or calls tornado. process. fork_processes) when you use the auto-Reload mode.

The automatic reloading function in debug mode can be used as an independent module in tornado. autoreload. The following two can be used in combination to provide extra robustness when syntax errors occur: Setting autoreload = True can detect file modifications during app runtime, as well as starting python-m tornado. autoreload myserver. to capture any syntax errors or other startup errors.

Overload will lose any Python interpreter command line parameters (-u) because it uses sys.exe cutable and sys. argv to re-Execute Python. In addition, modifying these variables will cause an overload error.

On Some platforms (including Windows and Mac OSX 10.6), the process cannot be updated in the same place. Therefore, when the code update is detected, the old service will exit and start a new service. This is already known to confuse some ides.

6. WSGI and Google App Engine
Tornado is usually run independently without a WSGI container. However, in some environments (such as Google App Engine), only WSGI is run, and applications cannot run their own services independently. In this case, Tornado supports a limited operation mode. asynchronous operations are not supported, but a subset of Tornado's functions is allowed in only WSGI environments. The following features are not supported in WSGI mode, including coroutine, @ asynchronous decorator, AsyncHTTPClient, auth module, and WebSockets.

You can use tornado. wsgi. WSGIAdapter to convert a Tornado Application into a WSGI Application. In this example, configure your WSGI container to send the application object:

import tornado.webimport tornado.wsgiclass MainHandler(tornado.web.RequestHandler):  def get(self):    self.write("Hello, world")tornado_app = tornado.web.Application([  (r"/", MainHandler),])application = tornado.wsgi.WSGIAdapter(tornado_app)

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Example of using MongoDB in Python Web framework Pylons
  • How to use multiple configuration files in the python WEB framework Flask
  • Share the simple Performance Test Results of common python web frameworks (including django, flask, bottle, tornado)
  • Introduction to the Flask signal mechanism (signals) in the Python Web framework
  • Use the Sina SAE cloud storage instance in the Python Web framework Flask
  • Use qiniu cloud storage instance in the Python Web framework Flask
  • An example of getting started with website development under the Python Web framework Flask
  • Brief Introduction to Python's lightweight web framework Bottle
  • Compile a Python web framework Model tutorial
  • Briefly introduce some key points of Self-writing the web framework in Python

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.