tornado异步web请求
2017-03-15 17:39
393 查看
1.为什么要使用异步web服务
使用异步非阻塞请求,并发处理更高效。
2.同步与异步请求比较
同步请求时,web服务器进程是阻塞的,也就是说当一个请求被处理时,服务器进程会被挂起直至请求完成。
异步请求时,web服务器进程在等待请求处理过程中,让I/O循环打开,以便服务于其他请求,请求处理完成后继续执行回调函数或生成器,而不再是等待请求过程中挂起进程。整个过程是异步的。
3.同步与异步请求示例
同步请求:
异步请求:
路由配置:
使用http_load工具(关于http_load的使用参见“http_load使用详解”)进行压力测试,结果如下:
同步压力测试:
异步压力测试:
对比可以看出,在60s时间内,并发请求数量为100的情况下,
同步请求只有27个请求响应,而异步请求达到了209个
4.异步请求使用说明
同步请求在请求完毕后,自动关闭连接。
异步请求保持连接开启,需要手动关闭连接。
tornado中使用@tornado.web.asynchronous装饰器作用是保持连接一直开启,
回调函数执行完毕后,调用finish方法来主动关闭连接。
5.异步生成器
上例中,是使用回调函数来做业务处理及关闭连接的。
回调函数的缺点是,可能引起回调深渊,系统将难以维护。如回调中调用回调。
tornado2.1引入了tornado.gen模块,可以更整洁地执行异步请求。
异步请求:
路由配置:
异步压力测试:
tornado.gen是一个生成器(关于生成器参见“python生成器,函数,数组” ),
yield关键字的作用是返回控制,异步任务执行完毕后,程序在yield的地方恢复。
可以看到使用生成器,异步后业务处理不是在回调函数中完成的,看起来像同步处理一样,代码逻辑更清晰。
使用生成器和回调函数异步请求是一样的。
6.异步请求的适用场景
请求处理逻辑复杂耗时,或长时间请求数据库的时候,异步请求可以大幅提升并发请求效率。
同时综合考虑缓存,业务逻辑放在客户端等手段,来缓解服务器压力。
参考资料:http://docs.pythontab.com/tornado/introduction-to-tornado/ch5.html
使用异步非阻塞请求,并发处理更高效。
2.同步与异步请求比较
同步请求时,web服务器进程是阻塞的,也就是说当一个请求被处理时,服务器进程会被挂起直至请求完成。
异步请求时,web服务器进程在等待请求处理过程中,让I/O循环打开,以便服务于其他请求,请求处理完成后继续执行回调函数或生成器,而不再是等待请求过程中挂起进程。整个过程是异步的。
3.同步与异步请求示例
同步请求:
class IndexHandler(tornado.web.RequestHandler): def get(self): client=tornado.httpclient.HTTPClient() response=client.fetch("http://test.com/list") self.write("success")
异步请求:
class IndexAsyncHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): client=tornado.httpclient.AsyncHTTPClient() client.fetch("http://test.com/list",callback=self.on_response) def on_response(self,response): self.write("success") self.finish()
路由配置:
(r'/test', test_async.IndexHandler), (r'/testasync', test_async.IndexAsyncHandle)
使用http_load工具(关于http_load的使用参见“http_load使用详解”)进行压力测试,结果如下:
同步压力测试:
[root@51dev http_load-12mar2006]# ./http_load -p 100 -s 60 url 27 fetches, 100 max parallel, 189 bytes, in 60 seconds 7 mean bytes/connection 0.45 fetches/sec, 3.15 bytes/sec msecs/connect: 0.113037 mean, 0.258 max, 0.021 min msecs/first-response: 31186.5 mean, 59721.3 max, 2246.32 min HTTP response codes: code 200 -- 27
异步压力测试:
209 fetches, 100 max parallel, 1463 bytes, in 60.0046 seconds 7 mean bytes/connection 3.48306 fetches/sec, 24.3814 bytes/sec msecs/connect: 0.0944641 mean, 0.387 max, 0.021 min msecs/first-response: 20088 mean, 30650 max, 10601.1 min HTTP response codes: code 200 -- 209
对比可以看出,在60s时间内,并发请求数量为100的情况下,
同步请求只有27个请求响应,而异步请求达到了209个
4.异步请求使用说明
同步请求在请求完毕后,自动关闭连接。
异步请求保持连接开启,需要手动关闭连接。
tornado中使用@tornado.web.asynchronous装饰器作用是保持连接一直开启,
回调函数执行完毕后,调用finish方法来主动关闭连接。
5.异步生成器
上例中,是使用回调函数来做业务处理及关闭连接的。
回调函数的缺点是,可能引起回调深渊,系统将难以维护。如回调中调用回调。
def get(self): client = AsyncHTTPClient() client.fetch("http://example.com", callback=on_response) def on_response(self, response): client = AsyncHTTPClient() client.fetch("http://another.example.com/", callback=on_response2) def on_response2(self, response): client = AsyncHTTPClient() client.fetch("http://still.another.example.com/", callback=on_response3) def on_response3(self, response): [etc., etc.]
tornado2.1引入了tornado.gen模块,可以更整洁地执行异步请求。
异步请求:
class IndexGenHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self): client=tornado.httpclient.AsyncHTTPClient() response=yield tornado.gen.Task(client.fetch,"http://test.com/list") self.write("success") self.finish()
路由配置:
(r'/testgen', test_async.IndexGenHandler),
异步压力测试:
207 fetches, 100 max parallel, 1449 bytes, in 60.0055 seconds 7 mean bytes/connection 3.44968 fetches/sec, 24.1478 bytes/sec msecs/connect: 0.113483 mean, 0.948 max, 0.024 min msecs/first-response: 20156.5 mean, 32294.2 max, 9607.34 min HTTP response codes: code 200 -- 207
tornado.gen是一个生成器(关于生成器参见“python生成器,函数,数组” ),
yield关键字的作用是返回控制,异步任务执行完毕后,程序在yield的地方恢复。
可以看到使用生成器,异步后业务处理不是在回调函数中完成的,看起来像同步处理一样,代码逻辑更清晰。
使用生成器和回调函数异步请求是一样的。
6.异步请求的适用场景
请求处理逻辑复杂耗时,或长时间请求数据库的时候,异步请求可以大幅提升并发请求效率。
同时综合考虑缓存,业务逻辑放在客户端等手段,来缓解服务器压力。
参考资料:http://docs.pythontab.com/tornado/introduction-to-tornado/ch5.html
相关文章推荐
- tornado异步web请求
- 使用Spring的WebAsyncTask异步请求时出错Can't set AsyncWebRequest with concurrent handling in progress
- iOS SDWebImage异步请求图片
- 使用tornado让你的请求异步非阻塞
- 使用tornado让你的请求异步非阻塞
- 利用tornado使请求实现异步非阻塞
- web 会话超时,请求(http请求和ajax异步请求)处理
- 解决:SDWebImage异步请求图片失败,图片没有加载出来
- 第二篇:Python高性能Web框架tornado源码剖析之待请求阶段 推荐
- 深入理解Tornado——一个异步web服务器
- Python Web框架Tornado的异步处理代码演示样例
- 使用tornado如何实现请求异步非阻塞?
- Python开发【Tornado】:异步Web服务(一)
- 说说web请求异步的处理
- 掌握 Ajax,第 2 部分: 使用 JavaScript 和 Ajax 发出异步请求 在 Web 请求中使用 XMLHttpRequest
- Web请求中同步与异步的区别
- 使用tornado让你的请求异步非阻塞
- 从零开始学 Web 之 Ajax(五)同步异步请求,数据格式
- 基元用户模式构造--互锁构造 Interlocked 实现的异步web请求实例
- 深入理解异步Web服务器 Tornado