Tips: 本文创建于2016年8月19日,已超过 2 年,内容或图片可能已经失效!

tornado 异步使用

同步

#!/usr/bin/evn python  
# coding:utf-8  
import tornado.httpserver  
import tornado.ioloop  
import tornado.options  
import tornado.web  
import tornado.httpclient  
  
import json  
  
from tornado.options import define, options  
define('port',default=9000,help='run on the given port', type=int)  
  
class IndexHandler(tornado.web.RequestHandler):  
    def get(self):  
        client = tornado.httpclient.HTTPClient()  
        response = client.fetch("http://ip.taobao.com//service/getIpInfo.php?ip=101.254.142.211")  
        body = json.loads(response.body)  
        self.write(body)  
  
if __name__ == "__main__":  
    tornado.options.parse_command_line()  
    app = tornado.web.Application(handlers=[(r'/', IndexHandler)])  
    http_server = tornado.httpserver.HTTPServer(app)  
    http_server.listen(options.port)  
    tornado.ioloop.IOLoop.instance().start() 

异步

#!/usr/bin/evn python  
# coding:utf-8  
import tornado.httpserver  
import tornado.ioloop  
import tornado.options  
import tornado.web  
import tornado.httpclient  
  
import json  
  
from tornado.options import define, options  
define('port',default=9000,help='run on the given port', type=int)  
  
class IndexHandler(tornado.web.RequestHandler):  
    @tornado.web.asynchronous #引用一个异步装饰器  
    def get(self):  
        client = tornado.httpclient.AsyncHTTPClient() #使用异步client  
        client.fetch("http://ip.taobao.com//service/getIpInfo.php?ip=101.254.142.211",callback=self.deal) # 使用回调  
  
    def deal(self, response): #添加一个回调函数  
        body = json.loads(response.body)  
        self.write(body)  
        self.finish() # 关闭链接  
  
if __name__ == "__main__":  
    tornado.options.parse_command_line()  
    app = tornado.web.Application(handlers=[(r'/', IndexHandler)])  
    http_server = tornado.httpserver.HTTPServer(app)  
    http_server.listen(options.port)  
    tornado.ioloop.IOLoop.instance().start()

异步生成器

#!/usr/bin/evn python  
# coding:utf-8  
import tornado.httpserver  
import tornado.ioloop  
import tornado.options  
import tornado.web  
import tornado.httpclient  
import tornado.gen  
import json  
  
from tornado.options import define, options  
define('port',default=9000,help='run on the given port', type=int)  
  
class IndexHandler(tornado.web.RequestHandler):  
    @tornado.web.asynchronous #引用一个异步装饰器  
    @tornado.gen.coroutine  
    def get(self):  
        client = tornado.httpclient.AsyncHTTPClient() #使用异步client  
        response = yield client.fetch("http://ip.taobao.com//service/getIpInfo.php?ip=101.254.142.211")  
  
        body = json.loads(response.body)  
        self.write(body)  
        self.finish() # 关闭链接  
  
if __name__ == "__main__":  
    tornado.options.parse_command_line()  
    app = tornado.web.Application(handlers=[(r'/', IndexHandler)])  
    http_server = tornado.httpserver.HTTPServer(app)  
    http_server.listen(options.port)  
    tornado.ioloop.IOLoop.instance().start()