Python-based Tornado Web Server for Comet

Source: Internet
Author: User

Generally, web applications use ajax Round Robin to obtain server updates. However, the round-robin method increases the load on the server, and the data obtained by many requests is not updated. These requests are meaningless and only increase the load on the server.

Using Comet techniques can solve the above problems to a certain extent. There are many implementations of Comet, most of which need to be implemented using specific HTTP Server. This article describes how to use the python-based Tornado Web Server to implement Server Side Comet. To stay close to the production environment, it also describes how Tornado works with Nginx.

Install

Go to http://www.tornadoweb.org/to download the latest tornado, and there are installation instructions.

Write the First Tornado Program

Edit comet. py with the following content:

#! /Usr/bin/env pythonimport tornado. httpserverimport tornado. ioloopimport tornado. webclass MainHandler (tornado. web. requestHandler): @ tornado. web. asynchronous # description 1 def get (self): some_async_func (callback = self. wait) def wait (self, result): if result: self. write (result) self. finish () # description 2 else: tornado. ioloop. IOLoop. instance (). add_timeout (time. time () + 0.5, lambda: some_async_func (callback = self. wait ))# Description 3 application = tornado. web. application ([(r "/", MainHandler),]) if _ name _ = "_ main _": http_server = tornado. httpserver. HTTPServer (application) http_server.listen (8999) tornado. ioloop. IOLoop. instance (). start () description 1: use this decorator to describe the get method non-blocking description 2: non-blocking requests must call self. finish () request Completion description 3: If sleep is required, use the add_timeout method of IOLoop of Tornado. Using other sleep methods such as time. sleep will block the current thread, while Tornado is a single-thread Server, which will cause other requests to be unable to be executed.

Run comet. py. A non-blocking server has been set up successfully.

Note that if the timeout mechanism is not added, the server will continue to execute it even if the client is disconnected. Therefore, you need to implement the timeout mechanism on your own.

Nginx Configuration

upstream frontends {    server 127.0.0.1:8999;}server {    listen 8888;    location / {        proxy_read_timeout 1800;        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;    }}

The reason for using proxy upstream to backend is to facilitate upstream load balancing. You only need to add several ip Ports to upstream. As mentioned above, Tornado is a single-threaded Server. It starts several Tornado processes with cpu cores and binds them to the cpu, which improves the machine throughput.

In particular, according to the timeout mechanism of the preceding application, proxy_read_timeout must be set, and proxy_read_timeout must be greater than the application timeout. Otherwise, 504 requests will be returned.

So far, the server of a Comet application is complete. Because Comet uses a persistent connection mechanism, pay special attention to the timeout mechanism for each link from the client to the real backend program, and the unreliability of network connections will be more affected, more boundary conditions need to be considered.

References:

  • Http://www.tornadoweb.org/documentation

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.