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

    异步非阻塞

    1、基本使用

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

    import tornado.web
    import tornado.ioloop
    from tornado import gen
    from tornado.concurrent import Future
    import time
    
    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()
    

       

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

    异步非阻塞体现在当在Tornaod等待用户向future对象中放置数据时,还可以处理其他请求。

    注意:在等待用户向future对象中放置数据或信号时,此连接是不断开的。

    2.同步阻塞和异步非阻塞对比

    同步阻塞:

    class SyncHandler(tornado.web.RequestHandler):
    
        def get(self):
            self.doing()
            self.write('sync')
    
        def doing(self):
            time.sleep(10)
    

    异步非阻塞:

    class AsyncHandler(tornado.web.RequestHandler):
        @gen.coroutine
        def get(self):
            future = Future()
            tornado.ioloop.IOLoop.current().add_timeout(time.time() + 5, self.doing)
            yield future
    
    
        def doing(self, *args, **kwargs):
            self.write('async')
            self.finish()
    

    3、httpclient类库

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

    class AsyncHandler(tornado.web.RequestHandler):
        @gen.coroutine
        def get(self):
            from tornado import httpclient
     
            http = httpclient.AsyncHTTPClient()
            yield http.fetch("http://www.google.com", self.endding)
     
     
        def endding(self, response):
            print(len(response.body))
            self.write('ok')
            self.finish()
    

      

  • 相关阅读:
    MEF(Managed Extensibility Framework ) 可控扩展框架
    如何打开ASP.NET Configuration页面
    [转贴]技术的乐趣
    ORM工具介绍 NHibernate, EntitySpaces, and LLBLGen Pro 功能分析
    Linq to SQL 学习路线图
    [转贴]What is AntiPattern 什么是反模式
    Master Data Management(MDM)主数据管理
    Introducing Unity Application Block
    C#2.0 C#3.0 语言特性
    javascript声明数组三种方式
  • 原文地址:https://www.cnblogs.com/wumingxiaoyao/p/6866147.html
Copyright © 2011-2022 走看看