码迷,mamicode.com
首页 > Web开发 > 详细

tornado异步web请求

时间:2017-03-15 18:39:59      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:3.1   color   需要   class   进程   div   and   web   finish   


1.为什么要使用异步web服务
使用异步非阻塞请求,并发处理更高效。

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使用详解” http://www.cnblogs.com/shijingjing07/p/6539179.html)进行压力测试,结果如下:
同步压力测试:

[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生成器,函数,数组” http://www.cnblogs.com/shijingjing07/p/6478539.html),
yield关键字的作用是返回控制,异步任务执行完毕后,程序在yield的地方恢复。
可以看到使用生成器,异步后业务处理不是在回调函数中完成的,看起来像同步处理一样,代码逻辑更清晰。
使用生成器和回调函数异步请求是一样的。

6.异步请求的适用场景
请求处理逻辑复杂耗时,或长时间请求数据库的时候,异步请求可以大幅提升并发请求效率。
同时综合考虑缓存,业务逻辑放在客户端等手段,来缓解服务器压力。

 

tornado异步web请求

标签:3.1   color   需要   class   进程   div   and   web   finish   

原文地址:http://www.cnblogs.com/shijingjing07/p/6555439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!