

tornado异步网络编程实践
source link: https://justpic.org/posts/2016/09/08/tornado-async-web/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

tornado异步网络编程实践
tornado异步网络编程实践
异步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;
}
}
}
Recommend
-
127
Thrift(Java版)到网络编程(三)—异步和NIO 前两篇啰嗦了很多,但也都是些基础玩法。作者也非常成功的绕过了非阻塞和异步这两个有点让人头疼的概念。写到第三篇,发现已经没法再回避了。所以,本文中作者试图以猿类最朴素的语言把这几个概...
-
89
协程定义: 协程,又称微线程,纤程。英文名Coroutine。 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕。所以子程序调用是通过栈实现的,一个线程就是执行一个子程序。子...
-
51
本系列文章将整理到我在GitHub上的《Java面试指南》仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下Star哈...
-
28
Java鐨凚IO鍜孨IO寰堥毦鎳傦紵鐢ㄤ唬鐮佸疄璺电粰浣犵湅锛屽啀涓嶆噦鎴戣浆琛岋紒-缃戠粶缂栫▼/涓撻」鎶€鏈尯 - 鍗虫椂閫氳寮€鍙戣€呯ぞ鍖! Java鐨凚IO鍜孨IO寰堥毦鎳傦紵鐢ㄤ唬鐮佸疄璺电粰浣犵湅锛屽啀涓嶆噦鎴戣浆琛岋紒
-
11
Tornado Tornado 是一款非阻塞可扩展的使用Python编写的web服务器和Python Web框架, 可以使用Tornado编写Web程序并不依赖任何web服务器直接提供高效的web服务.所以Tornado不仅仅是一个web框架而且还是一款可以...
-
13
Home Menu...
-
10
tornado原理介绍及异步非阻塞实现方式 以下内容根据自己实操和理解进行的整理,欢迎交流~ 在tornado的开发中,我们一般会见到以下四个组成部分。 ioloop: 同一个ioloop实例运...
-
5
关于Tornado:真实的异步和虚假的异步首页 - Python/2019-09-01...
-
7
使用异步非阻塞框架Tornado配合七牛云存储Api来异步切分上传文件首页 - Python/2019-12-15
-
8
Python3的原生协程(Async/Await)和Tornado异步非阻塞首页 - Python/2019-09-20
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK