Tornado(二)异步web

异步web请求

测试工具

Siege utility

从同步开始

#coding=utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
#接受命令行形式的一些参数

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        client = tornado.httpclient.HTTPClient()
        response = client.fetch("http://www.baidu.com")
        # 同步的相当于request
        self.write(response.body)

if __name__ == "__main__":
    tornado.options.parse_command_line()
    #处理命令行
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    #实例化application
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

同步测试结果

Transactions:                114 hits
Availability:             100.00 %
Elapsed time:               9.99 secs
Data transferred:           2.58 MB
Response time:              0.60 secs
Transaction rate:          11.41 trans/sec
Throughput:             0.26 MB/sec
Concurrency:                6.80
Successful transactions:         114
Failed transactions:               0
Longest transaction:            4.67
Shortest transaction:           0.01

基础异步调用

class IndexHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        client = tornado.httpclient.AsyncHTTPClient()
        client.fetch("http://www.baidu.com",
                callback=self.on_response)

    def on_response(self, response):
        self.write(response.body)
        self.finish()
异步测试结果
Transactions:                771 hits
Availability:              99.87 %
Elapsed time:               9.60 secs
Data transferred:          17.54 MB
Response time:              0.08 secs
Transaction rate:          80.31 trans/sec
Throughput:             1.83 MB/sec
Concurrency:                6.60
Successful transactions:         771
Failed transactions:               1
Longest transaction:            0.92
Shortest transaction:           0.01
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值