Python开发【Tornado】:异步Web服务(二)
真正的 Tornado 异步非阻塞
前言:
其中 Tornado 的定义是 Web 框架和异步网络库,其中他具备有异步非阻塞能力,能解决他两个框架请求阻塞的问题,在需要并发能力时候就应该使用 Tornado。
但是在实际使用过程中很容易把 Tornado 使用成异步阻塞框架,这样对比其他两大框架没有任何优势而言,本文就如何实现真正的异步非阻塞记录。
笔记:
- 默认情况下tornado是单线程阻塞模式,如果阻塞所有请求都需要等待
- tornado.web.asynchronous可以异步使用,得益于AsyncHTTPClient模块的配合使用,两者缺一不可
- tornado.gen.coroutine严重依赖第三方库的使用,如果没有第三方库的支持则依然是阻塞模式
- Tornado 提供了多种的异步编写形式:回调、Future、协程等,其中以协程模式最是简单和用的最多
- Tornado 实现异步的多种方式:coroutine配合第三方库、启用多线程、使用celery等
1、使用 gen.coroutine 异步编程
在 Tornado 中两个装饰器:
- tornado.web.asynchronous
- tornado.gen.coroutine
① asynchronous 装饰器是让请求变成长连接的方式,必须手动调用 self.finish()
才会响应
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
# bad
self.write("Hello, world")
asynchronous 装饰器不会自动调用self.finish()
,如果没有没有指定结束,该长连接会一直保持直到 pending 状态
所以正确是使用方式是使用了 asynchronous 需要手动 finish
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write("Hello, world")
self.finish()
② coroutine 装饰器是指定改请求为协程模式,说明白点就是能使用 yield
配合 Tornado 编写异步程序。
Tronado 为协程实现了一套自己的协议,不能使用 Python 普通的生成器。
在使用协程模式编程之前要知道如何编写 Tornado 中的异步函数,Tornado 提供了多种的异步编写形式:回调、Future、协程等,其中以协程模式最是简单和用的最多。
编写一个基于协程的异步函数同样需要 coroutine 装饰器
@gen.coroutine
def sleep(self):
yield gen.sleep(10)
raise gen.Return([1, 2, 3, 4, 5])
这就是一个异步函数,Tornado 的协程异步函数有两个特点:
- 需要使用 coroutine 装饰器
- 返回值需要使用
raise gen.Return()
当做异常抛出
返回值作为异常抛出是因为在 Python 3.2 之前生成器是不允许有返回值的。
使用过 Python 生成器应该知道,想要启动生成器的话必须手动执行 next()
方法才行,所以这里的 coroutine 装饰器的其中一个作用就是在调用这个异步函数时候自动执行生成器。
使用 coroutine 方式有个很明显是缺点就是严重依赖第三方库的实现,如果库本身不支持 Tornado 的异步操作再怎么使用协程也是白搭依然会是阻塞的,放个例子感受一下
import time
import logging
import tornado.ioloop
import tornado.web
import tornado.options
from tornado import gen tornado.options.parse_command_line() class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write("Hello, world")
self.finish() class NoBlockingHnadler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
yield gen.sleep(2)
self.write('Blocking Request') class BlockingHnadler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
time.sleep(2)
self.write('Blocking Request') def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/block", BlockingHnadler),
(r"/noblock", NoBlockingHnadler),
], autoreload=True) if __name__ == "__main__":
app = make_app()
app.listen(8001)
tornado.ioloop.IOLoop.current().start()
当我们使用 yield gen.sleep(10)
这个异步的 sleep 时候,请求是不阻塞的。
[root@localhost ~]# siege http://192.168.0.2:8001/noblock -c100 -t10s
** SIEGE 3.0.
** Preparing concurrent users for battle.
The server is now under siege...
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.88 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.88 secs: bytes ==> GET /noblock
HTTP/1.1 1.88 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.87 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.85 secs: bytes ==> GET /noblock
HTTP/1.1 1.86 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock
HTTP/1.1 1.83 secs: bytes ==> GET /noblock
HTTP/1.1 1.84 secs: bytes ==> GET /noblock Lifting the server siege... done. Transactions: hits
Availability: 100.00 %
Elapsed time: 9.35 secs
Data transferred: 0.01 MB
Response time: 1.85 secs
Transaction rate: 36.04 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 66.55
Successful transactions:
Failed transactions:
Longest transaction: 1.88
Shortest transaction: 1.83 FILE: /root/siege.log
You can disable this annoying message by editing
the .siegerc file in your home directory; change
the directive 'show-logfile' to false.
基准测试输出
测试结果:100并发,请求10s,共发送337次请求,平均每秒处理36.04次请求,很明显程序是非阻塞的
当使用 time.sleep(10)
时候请求时阻塞的。
[root@localhost ~]# siege http://192.168.0.2:8001/block -c100 -t10s
** SIEGE 3.0.
** Preparing concurrent users for battle.
The server is now under siege...
HTTP/1.1 1.84 secs: bytes ==> GET /block
HTTP/1.1 3.68 secs: bytes ==> GET /block
HTTP/1.1 5.51 secs: bytes ==> GET /block
HTTP/1.1 7.34 secs: bytes ==> GET /block
HTTP/1.1 9.17 secs: bytes ==> GET /block Lifting the server siege... done. Transactions: hits
Availability: 100.00 %
Elapsed time: 9.23 secs
Data transferred: 0.00 MB
Response time: 5.51 secs
Transaction rate: 0.54 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 2.98
Successful transactions:
Failed transactions:
Longest transaction: 9.17
Shortest transaction: 0.00 FILE: /root/siege.log
You can disable this annoying message by editing
the .siegerc file in your home directory; change
the directive 'show-logfile' to false.
基准测试输出
测试结果:100并发,请求10s,共发送5次请求,平均每秒处理0.5次请求,和time.sleep 2秒钟预先的效果是一致的。
gen.coroutine
在 Tornado 3.1 后会自动调用 self.finish()
结束请求,可以不使用 asynchronous
装饰器。
所以这种实现异步非阻塞的方式需要依赖大量的基于 Tornado 协议的异步库,使用上比较局限,好在还是有一些可以用的异步库
2、启用线程的处理异步编程
注:使用 gen.coroutine
装饰器编写异步函数,如果库本身不支持异步,那么响应任然是阻塞的。
在 Tornado 中有个装饰器能使用 ThreadPoolExecutor
来让阻塞过程编程非阻塞,其原理是在 Tornado 本身这个线程之外另外启动一个线程来执行阻塞的程序,从而让 Tornado 变得非阻塞,根本原理就是启用多个线程处理,处理后的线程并不会自动关闭
import time
import logging
import tornado.ioloop
import tornado.web
import tornado.options
from tornado import gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor tornado.options.parse_command_line() class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write("Hello, world")
self.finish() class NoBlockingHnadler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(4) # 启动4个线程处理阻塞请求 def sleep(self, second):
time.sleep(second)
return second @run_on_executor
def get(self):
second = yield self.sleep(5)
self.write('noBlocking Request: {}'.format(second)) def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/noblock", NoBlockingHnadler),
], autoreload=True) if __name__ == "__main__":
app = make_app()
app.listen(8000)
tornado.ioloop.IOLoop.current().start()
hreadPoolExecutor
是对标准库中的 threading 的高度封装,利用线程的方式让阻塞函数异步化,解决了很多库是不支持异步的问题。
但是与之而来的问题是,如果大量使用线程化的异步函数做一些高负载的活动,会导致该 Tornado 进程性能低下响应缓慢,这只是从一个问题到了另一个问题而已。
所以在处理一些小负载的工作,是能起到很好的效果,让 Tornado 异步非阻塞的跑起来。
但是明明知道这个函数中做的是高负载的工作,那么你应该采用另一种方式,使用 Tornado 结合 Celery 来实现异步非阻塞。
[root@localhost ~]# siege http://192.168.0.2:8001/noblock -c10 -t10s
** SIEGE 3.0.
** Preparing concurrent users for battle.
The server is now under siege...
HTTP/1.1 4.59 secs: bytes ==> GET /noblock
HTTP/1.1 4.59 secs: bytes ==> GET /noblock
HTTP/1.1 4.60 secs: bytes ==> GET /noblock
HTTP/1.1 4.60 secs: bytes ==> GET /noblock
HTTP/1.1 9.18 secs: bytes ==> GET /noblock
HTTP/1.1 8.18 secs: bytes ==> GET /noblock
HTTP/1.1 8.18 secs: bytes ==> GET /noblock
HTTP/1.1 8.18 secs: bytes ==> GET /noblock Lifting the server siege... done. Transactions: hits
Availability: 100.00 %
Elapsed time: 9.42 secs
Data transferred: 0.00 MB
Response time: 6.51 secs
Transaction rate: 0.85 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 5.53
Successful transactions:
Failed transactions:
Longest transaction: 9.18
Shortest transaction: 0.00 FILE: /root/siege.log
You can disable this annoying message by editing
the .siegerc file in your home directory; change
the directive 'show-logfile' to false.
基准测试输出
3、基于 Celery 的异步编程
Celery 是一个简单、灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的任务队列,同时也支持任务调度。
Celery 并不是唯一选择,你可选择其他的任务队列来实现,但是 Celery 是 Python 所编写,能很快的上手,同时 Celery 提供了优雅的接口,易于与 Python Web 框架集成等特点。
与 Tornado 的配合可以使用 tornado-celery
,该包已经把 Celery 封装到 Tornado 中,可以直接使用(本节暂未测试,文章转载)
实际测试中,由于 tornado-celery 很久没有更新,导致请求会一直阻塞,不会返回 解决办法是: 把 celery 降级到 3.1 pip install celery==3.1
把 pika 降级到 0.9.14 pip install pika==0.9.14
Tornado服务端:
import time
import logging
import tornado.ioloop
import tornado.web
import tornado.options
from tornado import gen import tcelery, tasks tornado.options.parse_command_line()
tcelery.setup_nonblocking_producer() class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write("Hello, world")
self.finish() class CeleryHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
response = yield gen.Task(tasks.sleep.apply_async, args=[5])
self.write('CeleryBlocking Request: {}'.format(response.result)) def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/celery-block", CeleryHandler),
], autoreload=True) if __name__ == "__main__":
app = make_app()
app.listen(8000)
tornado.ioloop.IOLoop.current().start()
celery调度端:
import os
import time
from celery import Celery
from tornado import gen celery = Celery("tasks", broker="amqp://")
celery.conf.CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND', 'amqp') @celery.task
def sleep(seconds):
time.sleep(float(seconds))
return seconds if __name__ == "__main__":
celery.start()
Celery 的 Worker 运行在另一个进程中,独立于 Tornado 进程,不会影响 Tornado 运行效率,在处理复杂任务时候比进程模式更有效率。
总结
推荐使用线程和 Celery 的模式进行异步编程,轻量级的放在线程中执行,复杂的放在 Celery 中执行。当然如果有异步库使用那最好不过了。目前没有找到最佳的异步非阻塞的编程模式,可用的异步库比较局限,只有经常用的,个人编写异步库比较困难。
Python 3 中可以把 Tornado 设置为 asyncio 的模式,这样就使用 兼容 asyncio 模式的库,这应该是日后的方向。
Reference
- http://www.tornadoweb.org/en/stable/
- https://github.com/mher/tornado-celery
- https://github.com/tornadoweb/tornado/wiki/Links
《Tornado异步一》------跳转
Python开发【Tornado】:异步Web服务(二)的更多相关文章
- tornado之异步web服务二
前面介绍了异步方法带来的显著性能提升.我们来看下异步使用的方法. 1 首先对于get方法使用tornado.web.asynchronous进行装饰.当使用tornado.web.asynchrono ...
- Python开发【Tornado】:异步Web服务(一)
异步Web服务 前言: 到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado ...
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...
- 第五章:异步Web服务
到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado受到最多关注的功能是其异 ...
- tornado异步web请求
1.为什么要使用异步web服务使用异步非阻塞请求,并发处理更高效. 2.同步与异步请求比较同步请求时,web服务器进程是阻塞的,也就是说当一个请求被处理时,服务器进程会被挂起直至请求完成. 异步请求时 ...
- 如何设计一个异步Web服务——任务调度
接上一篇<如何设计一个异步Web服务——接口部分> Application已经将任务信息发到了Service服务器中,接下来,Service服务器改如何对自身的资源进行合理分配以满足App ...
- 如何设计一个异步Web服务——接口部分
需求比较简单,提供一个异步Web服务供使用者调用.比如说,某应用程序需要批量地给图片加lomo效果.由于加lomo效果这个操作非常消耗CPU资源,所以我们需要把这个加lomo效果的程序逻辑放到一台单独 ...
- asyncio创建协程解析——分析廖雪峰的Python教程之创建WEB服务(转)
第一步,搭建开发环境 所需第三方库: aiohttp,异步 Web 开发框架:jinja2,前端模板引擎:aiomysql,异步 mysql 数据库驱动 所需内置库: logging,系统日志:asy ...
- tornado之异步web服务一
大部分Web应用(包括我们之前的例子)都是阻塞性质的,也就是说当一个请求被处理时,这个进程就会被挂起直至请求完成.在大多数情况下,Tornado处理的Web请求完成得足够快使得这个问题并不需要被关注. ...
随机推荐
- erlang的简单模拟半包的产生
gen_tcp:linsten()/2使用的是{packet,2/4/8},则gen_tcp模块在接受或者发送时自动除去包头或者自动加上包头. 本例中使用的是{packet,0}. -module( ...
- ajax basic 认证
//需要Base64见:http://www.webtoolkit.info/javascript-base64.html function make_base_auth(user, password ...
- c++ double float 数值比较
浮点数在内存中的存储机制和整型数不同,其有舍入误差,在计算机中用近似表示任意某个实数.具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基 ...
- jQuery动态生成Bootstrap表格
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- ubuntu-12.04.5-desktop-amd64.iso:ubuntu-12.04.5-desktop-amd64:安装Oracle11gR2
ubuntu 桌面版的安装不介绍. 如何安装oracle:核心步骤和关键点. ln -sf /bin/bash /bin/sh ln -sf /usr/bin/basename /bin/basena ...
- python语言简介、解释器、字符编码介绍
一.为什么要选择python作为学习语言: 各个语言的对比: C和python.java.C#等 C语言:代码编译得到机器码,机器码在处理器上直接执行,每一条指令控制cpu工作 其他语言:代码编译得到 ...
- ARM漏洞
Google安全团队Project Zero公布了多个高危漏洞,称这些漏洞几乎影响到了市面上所有的微处理器,AMD.ARM还是英特尔的处理器都难以幸免,围绕这些处理器打造的操作系统和硬件设备也会受到影 ...
- jQuery子页面获取父页面元素
$("input[type='checkbox']:checked",window.opener.document);//适用于打开窗口的父页面元素获取 $("input ...
- 《转》python学习(4)对象
转自http://www.cnblogs.com/BeginMan/p/3160044.html 一.学习目录 1.pyhton对象 2.python类型 3.类型操作符与内建函数 4.类型工厂函数 ...
- poj_2286 IDA*
题目大意 给定一个由数字组成的#字型网格,和一定的移动规则,问最少需要多少次移动才能达到要求的结果. 题目分析 要求最少需要几步到达结果,可以考虑广度优先搜索算法,或者迭代加深深度优先搜索(IDA*) ...