zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    SSH免密登陆
    Linux服务器绑定多网卡IP
    搭建简易网站
    Linux中raid磁盘阵列
    Linux中防火墙命令
    Linux中LVM逻辑卷管理
    Linux中fdisk分区
    Linux计划任务
    Linux基础命令(三)
    Linux基础命令(二)
  • 原文地址:https://www.cnblogs.com/weilaixiaochaoren/p/10276105.html
Copyright © 2011-2022 走看看