thinking about tornado asynchronous

2016年09月08日

C10K问题:

异步tornado请求: 底层建立socket通讯, 服务器采用epoll方案,即时返回请求,轮询到后台任务结束时,通过socket发送数据到客户端 理解异步的时候: http请求断了,tcp层面上的socket连接还在保持, 客户端通过再次发送http请求,可以得到后台处理的结果,long pooling技术 socket套接字主要由ip地址、tcp/ip协议和端口号决定,利用epoll的套接字复用技术,能够保证任务请求能够发送到正确的客户端受伤, server { listen 80; server_name yourdomain.tld; location / { try_files $uri @tornado; } location @tornado { proxy_pass http://localhost:8888; } } user nginx; worker_processes 1;

error_log /var/log/nginx/error.log; pid /var/run/nginx.pid;

events { worker_connections 1024; use epoll; }

http { # 在upstream中列出所有的tornado server,当然如果你要做不同的路由跳转的时候可以定义多个upstream 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;
    }
} }

user nginx; worker_processes 1;

error_log /var/log/nginx/error.log; pid /var/run/nginx.pid;

events { worker_connections 1024; use epoll; }

http { # 在upstream中列出所有的tornado server,当然如果你要做不同的路由跳转的时候可以定义多个upstream 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;
    }
} } 参考 http://golubenco.org/understanding-the-code-inside-tornado-the-asynchronous-web-server-powering-friendfeed.html http://zqdevres.qiniucdn.com/data/20100927213110/index.html http://xiaorui.cc/2016/06/26/%E8%AE%B0%E4%B8%80%E6%AC%A1%E5%8E%8B%E6%B5%8B%E5%BC%95%E8%B5%B7%E7%9A%84nginx%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98/ http://stackoverflow.com/questions/32261494/should-we-do-database-calls-async-with-tornado http://www.dongwm.com/archives/shi-yong-tornadorang-ni-de-qing-qiu-yi-bu-fei-zu-sai/ http://qinxuye.me/article/ways-to-continual-sync-browser-and-server/