Tornado-第二篇-异步非阻塞
manage.py
from tornado.ioloop import IOLoop from tornado.web import RequestHandler from tornado.web import Application from tornado import gen from tornado import httpclient class HomeHandler(RequestHandler): def get(self, *args, **kwargs): name = self.get_query_argument('name') print('用户%s的请求来了', name) import time time.sleep(20) self.write('这是结果') class AsyncHandler(RequestHandler): # 1.加gen.coroution装饰器 @gen.coroutine def get(self, *args, **kwargs): name = self.get_query_argument('name') print('用户%s的去请求来了' % name) http = httpclient.AsyncHTTPClient() # 2.返回一个Future对象 yield http.fetch('http://www.google.com', self.done) def done(self, response): print(response) self.write('ok') self.finish() application = Application([ ('/home', HomeHandler), ('/async', AsyncHandler), ]) if __name__ == '__main__': application.listen(8888) IOLoop.instance().start()