zoukankan      html  css  js  c++  java
  • Tornado之异步非阻塞

     同步模式:同步模式下,只有处理完前一个任务下一个才会执行

    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            time.sleep(10)
            self.write("Hello, world")
    
    application = tornado.web.Application([
        (r"/index", MainHandler),
    ])
    if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

    tornado如何使用异步非阻塞:

      -Future对象

      -IO事件循环

      -生成器

    异步模式

    装饰器 + Future 从而实现Tornado的异步非阻塞

    class AsyncHandler(tornado.web.RequestHandler):
     
        @gen.coroutine
        def get(self):
            future = Future()
            future.add_done_callback(self.doing)
            yield future
            #
            #tornado.ioloop.IOLoop.current().add_future(future,self.doing)
            # yield future
     
        def doing(self,*args, **kwargs):
            self.write('async')
            self.finish()
    application = tornado.web.Application([
        (r"/index",AsyncHandler),
    ])
    
    
    if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
     

    GET请求--->方法被@gen.coroutine装饰且yield 一个 Future对象-->Tornado等待----->用户向future对象中放置数据或者发送信号,如果获取到数据或信号之后,就开始执行doing方法。

    异步非阻塞体现在当在Tornaod等待用户向future对象中放置数据时,还可以处理其他请求。在等待用户向future对象中放置数据或信号时,此连接是不断开的。

    简单实现的异步非阻塞

    # Author:song
    import tornado.web
    import tornado.ioloop
    from tornado import gen
    from tornado.concurrent import Future
    import time
    
    class MainHandler(tornado.web.RequestHandler):
        @gen.coroutine
        def get(self):
            future = Future()
            tornado.ioloop.IOLoop.current().add_timeout(time.time() + 5, self.done)#模拟出现阻塞
            yield future
    
        def done(self, *args, **kwargs):
            self.write('async')
            self.finish()
    
    
    class LoginHandler(tornado.web.RequestHandler):
        def get(self):
            self.write('登录')
        def post(self, *args, **kwargs):
            print(self.get_argument('user'))
            self.redirect('https://home.cnblogs.com/u/master-song/')
    
    application = tornado.web.Application([
        (r"/main", MainHandler),
        (r"/login", LoginHandler),
    ])
    
    if __name__ == "__main__":
        application.listen(8888)
        tornado.ioloop.IOLoop.instance().start()
    tornado_async

    Tornado提供了httpclient类库用于发送Http请求,其配合Tornado的异步非阻塞使用

    class MainHandler(tornado.web.RequestHandler):
    
        @gen.coroutine
        def get(self):
            http = httpclient.AsyncHTTPClient()
            yield http.fetch("http://www.google.com", self.done)
    
        def done(self, response):
        
            self.finish()   
    tornado_async

    future=Future()

    1.挂起当前请求,线程可以处理其他请求

    2.future设置值,当前挂起请求返回

  • 相关阅读:
    LeetCode Linked List Cyle
    【小程序】使用uni-app搭建小程序环境---滑块组件
    【小程序】使用uni-app搭建小程序环境---图片懒加载
    【小程序】使用uni-app搭建小程序环境---列表上拉加载更多
    【小程序】使用uni-app搭建小程序环境---页面跳转
    【小程序】使用uni-app搭建小程序环境--轮播图
    【小程序】使用uni-app搭建小程序环境---路由配置及页面跳转获取参数
    【小程序】使用uni-app搭建小程序环境---工程结构和页面管理
    【小程序】使用uni-app搭建小程序环境---尺寸单位
    【小程序】使用uni-app搭建小程序环境---css变化
  • 原文地址:https://www.cnblogs.com/master-song/p/9438922.html
Copyright © 2011-2022 走看看