tornado 异步 asynchronous 学习记录
🕝 by pyList at 2013-09-28 13:42
Tornado 的特点就是异步非阻塞。下面记录一下tornado 异步 asynchronous 的学习理解。
non-blocking I/O, 我简单的理解是“允许某一个操作可以继续进行,而不必等待某一资源的响应,预提供一个回调函数,用于处理、响应该资源的结果(当该资源返回相关内容的时候)”
对比异步I/O,我们最常见的就是同步 I/O(线性编程),一次请求访问另一个资源,必须等待该资源的成功返回,方可进行下一步操作,如果该资源无响应(或异常),程序就终止(受限)于此。
sync/async的大致对比
图片:sync-async.jpg
一个普通的web app,将会被用于异步请求的调用。
python code: 普通的web app
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
'''
The below is only a testing which will be exected to kill time,
and then we can find the asynchronous effect from the front page.
'''
for i in range(1, 100000):
print "kill time"
self.write("hello")
settings = {
#"debug": True,
}
application = tornado.web.Application([
(r"/async-sync-test/", MainHandler),
], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8084)
tornado.ioloop.IOLoop.instance().start()
一次异步请求调用
python code: 异步请求调用
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.httpclient import AsyncHTTPClient
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://localhost:8084/async-sync-test/", callback=self._test_callback)
self.write("Hello to the Tornado world! ")
'''
Flushes the current output buffer to the network.
'''
self.flush()
'''
_test_callback is a callback function used for the processing of the response from the async request
'''
def _test_callback(self, response):
self.write(response.body)
'''
refer the offical document, we are responsible for the invocation of the finish function in the async case.
'''
self.finish()
settings = {
#"debug": True,
}
application = tornado.web.Application([
(r"/", MainHandler),
], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8083)
tornado.ioloop.IOLoop.instance().start()
本文网址: https://pylist.com/t/1380346956 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
相关推荐
小工具
标签
示例
asynchronous
tornado
gen
python
uuid
模块
记录
学习
handler
线程
异步
委派
网址
检测
目标
comet
构建
应用
一个
coroutine
yield
例子
template
golang
tenjin
性能
优雅
重启
完整
sae
pytenjin
websocket
long
poll
推送
硬件加速
ubnt
er
固件
路由
虚拟机
virtualbox
很慢
网络
解决
text
sublime
内存
暴涨
乱码
vim
linux
中文
centos
不可
openwrt
usb
上网卡
共享
bbr
开启
dbutils
连接池
mysql
数据
国内
form
data
post
上传
u盘
mac
diskutil
格式化
顽固
最新版
wndr4300
升级
sqlite3
time
timestamp
小结
时间
默认值
struct
设置
failed
load
devtools
sourcemap
chrome
自动更新
microsoft
关闭
app
store
登录
未知
静音
风扇
主机
笔记本
改造
低功耗
web
爬虫
服务器
组装
微信
尝鲜
视频
体验
cpu
debian
ubuntu
查看
温度
server
浏览
google
身份验证
authenticator
迁移
手机
webdriver
微博
selenium
gnu
安装
系统启动
编译
宅家
坑记
屏幕
动手
nginx
quic
抢先
最近发表
- Chrome 控制台 DevTools failed to load SourceMap 警告的消除方法
- Mac 关闭 Microsoft 自动更新
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 老笔记本改造为无风扇静音主机方案
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 微信视频号尝鲜体验
- Ubuntu/Debian 查看CPU温度的方法
- 在Ubuntu/debian Server 系统使用Chrome 无头浏览模式
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- ubuntu/debian 下自行编译 OpenWRT 固件
- 宅家自己动手换手机屏幕掉坑记
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
最近浏览
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- VirtualBox 虚拟机里网络很慢的解决方法
- 解决Sublime text 内存暴涨的问题
- linux vim 中文显示乱码的解决方法
- virtualbox centos 网络不可用问题的解决
- Openwrt 使用USB 4G 上网卡共享网络
- Openwrt 路由上开启BBR
- Python 用DBUtils 建立mysql 数据连接池
- OpenWrt 国内源
- python form-data post上传数据简便方法
- 在终端使用Mac diskutil 命令格式化顽固U盘
- WNDR4300 固件升级到 OpenWrt 最新版
- python SQLite3 连接池
- golang timestamp time 时间戳小结
- go struct 设置默认值
- Chrome 控制台 DevTools failed to load SourceMap 警告的消除方法