サーバについて-構成tornadoのインストール

6479 ワード

tornadoはpythonベースのもう一つのサーバフレームワークであり、djangoのように豊富な「ステーション構築ツール」を提供することはできませんが、epollのサポートにより、非ブロック特性が魅力的になります.以下にも簡単にインストール方法を記録します.
まずインストールtornadoをダウンロードし、実行するには次の2つのライブラリをインストールする必要があります.
1,pycurl
2, mysql-python  
 (python setup.py install)
 
tornadoはdjangoのmanageを提供していません.py,django-admin.pyなどのツールは、直接次のように符号化されます.
list_publishers.html(テンプレート)
<table>
        {% for p in publishers %}
        <tr>
        <td>Id #{{ p[0] }}</td>
        <td>Name #{{ p[1] }}</td>
        </tr>
        {% end %}
</table>

 web.py(コード)
import tornado.ioloop
import tornado.web
import tornado.database
import sqlite3

def _execute(query):
        dbPath = '/home/ciaos/django.sqlite3'
        connection = sqlite3.connect(dbPath)
        cursorobj = connection.cursor()
        try:
                cursorobj.execute(query)
                result = cursorobj.fetchall()
                connection.commit()
        except Exception:
                raise
        connection.close()
        return result

class Main(tornado.web.RequestHandler):
    def get(self):
        self.write("Main")

class ListPublishers(tornado.web.RequestHandler):
    def get(self):
        query = ''' select * from books_publisher '''
        publishers = _execute(query)
        self.render("list_publishers.html", publishers=publishers)

application = tornado.web.Application([
    (r"/", Main),
    (r"/publishers" , ListPublishers),
],debug=True)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

簡単にabで性能をテストして、djangoの性能より倍高い様子です(ただし、2つのプロジェクトがデータベースを操作する方法が少し異なるため、tornadoのsqliteに対するオリジナルのサポートが見つからなかったため、説得力がありません)
ciaos@linux-53dr:~/mysite2> sudo /usr/sbin/ab2 -c 10 -n 10000 http://127.0.0.1:8888/publishers
root's password:
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        TornadoServer/2.0
Server Hostname:        127.0.0.1
Server Port:            8888

Document Path:          /publishers
Document Length:        63 bytes

Concurrency Level:      10
Time taken for tests:   21.306 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2190000 bytes
HTML transferred:       630000 bytes
Requests per second:    469.35 [#/sec] (mean)
Time per request:       21.306 [ms] (mean)
Time per request:       2.131 [ms] (mean, across all concurrent requests)
Transfer rate:          100.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       7
Processing:     4   21   3.4     20      53
Waiting:        0   20   3.4     19      52
Total:          7   21   3.5     20      56

Percentage of the requests served within a certain time (ms)
  50%     20
  66%     21
  75%     21
  80%     22
  90%     23
  95%     25
  98%     30
  99%     42
 100%     56 (longest request)
生産環境の構成は参照可能
Tornadoの概要の紹介
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:8888;
        server 127.0.0.1:8000;
    }

    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 false;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}

簡単な構成で、古いphp-cgiプログラミングから遠ざかることができます.簡単なmvcコードは添付ファイルを参照