本文共 3911 字,大约阅读时间需要 13 分钟。
#coding=utf-8import timeimport tornado.httpserverimport tornado.webimport tornado.ioloopfrom tornado.options import define, options#定义一个默认的端口define("port", default=4000, help="run port ", type=int)class IndexHandler(tornado.web.RequestHandler): def get(self): #设置休眠时间 time.sleep(5) self.write("我是休眠的")if __name__ == "__main__": options.parse_command_line() app = tornado.web.Application( handlers=[ (r'/', IndexHandler) ], template_path='templates', static_path='static', debug=True ) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) print 'start server...' tornado.ioloop.IOLoop.instance().start()
import tornado.httpclient
3、在get或post方法里 实例化异步客户端
client = tornado.httpclient.AsyncHTTPClient()
4、通过异步客户端的fetch
方法请求一个耗时的网络接口,并传入callback
回调函数
self.finish()
6、给异步的方法加上装饰器@tornado.web.asynchronous
#coding=utf-8import timeimport tornado.httpclientimport tornado.httpserverimport tornado.webimport tornado.ioloopfrom tornado.options import define, options#定义一个默认的端口define("port", default=8000, help="run port ", type=int)#使用回调函数实现异步编程class IndexHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): client = tornado.httpclient.AsyncHTTPClient() client.fetch("http://xx.xx.xx.xxx:4000",callback=self.on_response) self.write("我是主页") def on_response(self,response): print response self.write(response.body) self.finish()if __name__ == "__main__": options.parse_command_line() app = tornado.web.Application( handlers=[ (r'/', IndexHandler) ], template_path='templates', static_path='static1', debug=True ) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) print 'start server...' tornado.ioloop.IOLoop.instance().start()
1、导包
import tornado.genimport tornado.httpclient
2、书写路由视图函数
3、在get
或post
方法里 实例化异步客户端
client = tornado.httpclient.AsyncHTTPClient()
4、使用yield
返回一个任务,需要传入异步客户端fetch
方法,和耗时的url
作为参数
response = yield tornado.gen.Task(client.fetch, "http://xx.xx.xx.xxx:8000/sync?id=2")
yield
返回的结果response
6、给get
或post
方法添加两个装饰器
@tornado.web.asynchronous@tornado.gen.coroutine
7、具体视图代码
#使用协程实现异步编程class IndexHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): client = tornado.httpclient.AsyncHTTPClient() response = yield tornado.gen.Task(client.fetch,"http://xx.xx.xx.xxx:4000") self.write("我是主页") self.write(response.body)
1、导包
import tornado.genimport tornado.httpclient
2、书写路由视图函数
yield
关键字返回我们自定义的函数4、处理yield
返回的结果response
self.write(response.body)
5、书写自定义函数
1、在函数中实例化异步客户端
client = tornado.httpclient.AsyncHTTPClient()
2、使用yield
返回一个任务,需要传入异步客户端fetch
方法,和耗时的url
作为参数
response = yield tornado.gen.Task(client.fetch, "http://xx.xx.xx.xxx:4000")
3、处理通过raise
返回的结果
raise tornado.gen.Return(response)
4、在自定义函数上加装饰符
@tornado.gen.coroutine
6、具体代码
class IndexHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): response = yield self.myfunc() self.write(response.body) @tornado.gen.coroutine def myfunc(self): client = tornado.httpclient.AsyncHTTPClient() response = yield tornado.gen.Task(client.fetch,"http://xx.xx.xx.xxx:4000/") raise tornado.gen.Return(response)
1、在虚拟环境下安装模块
pip install futures
2、导入模块
from tornado.concurrent import run_on_executorfrom concurrent.futures import ThreadPoolExecutorimport tornado.gen
3、书写视图函数
4、通过yield
关键字返回我们自定义的函数
response = yield self.myfunc()
5、处理yield
返回的结果response
self.write(response.body)
6、书写自定义函数myfunc
1、在函数中使用requests
模块发送get
请求
response = requests.get("http://xx.xx.xx.xxx:4000")
转载地址:http://ntvf.baihongyu.com/