• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評(píng)論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            python Tornado異步服務(wù)器

            Tornado基于Epoll(unix為kqueue)的異步網(wǎng)絡(luò)IO,Tornado的異步包括兩個(gè)方面,異步服務(wù)端和異步客戶端。無論服務(wù)端和客戶端,具體的異步模型又可以分為回調(diào)(callback)和協(xié)程(coroutine)
            裝飾器說明:
            @tornado.web.asynchronous 裝飾器適用于callback-style的異步方法,如果是協(xié)程則可以用@tornado.gen.coroutine來修飾。
            對(duì)于用@tornado.web.asynchronous 修飾的異步方法,需要主動(dòng)self.finish()來結(jié)束該請(qǐng)求,普通的方法(get()等)會(huì)自動(dòng)結(jié)束請(qǐng)求在方法返回的時(shí)候。

            服務(wù)端異步方式:有兩種,一種是yield掛起函數(shù),另外一種就是使用類線程池的方式 還有一種Future
            1、yield:掛起函數(shù)協(xié)程,盡管沒有block主線程,因?yàn)樾枰幚矸祷刂担瑨炱鸬巾憫?yīng)執(zhí)行還是有時(shí)間等待
             1 class AsyncTaskHandler(tornado.web.RequestHandler):
             2   @tornado.web.asynchronous
             3   @tornado.gen.coroutine
             4   def get(self, *args, **kwargs):
             5     # yield 結(jié)果
             6     response = yield tornado.gen.Task(self.ping, ' www.google.com')
             7     print 'response', response
             8     self.finish('hello')
             9  
            10   @tornado.gen.coroutine
            11   def ping(self, url):
            12     os.system("ping -c 2 {}".format(url))
            13     return 'after'
            2、線程池:
             1 from concurrent.futures import ThreadPoolExecutor
             2  
             3 class FutureHandler(tornado.web.RequestHandler):
             4   executor = ThreadPoolExecutor(10)
             5  
             6   @tornado.web.asynchronous
             7   @tornado.gen.coroutine
             8   def get(self, *args, **kwargs):
             9  
            10     url = 'www.google.com'
            11     tornado.ioloop.IOLoop.instance().add_callback(functools.partial(self.ping, url))
            12     self.finish('It works')
            13  
            14   @tornado.concurrent.run_on_executor
            15   def ping(self, url):
            16     os.system("ping -c 2 {}".format(url))

            要返回值也很容易。再切換一下使用方式接口。使用tornado的gen模塊下的with_timeout功能(這個(gè)功能必須在tornado>3.2的版本)。
             1 class Executor(ThreadPoolExecutor):
             2   _instance = None
             3  
             4   def __new__(cls, *args, **kwargs):
             5     if not getattr(cls, '_instance', None):
             6       cls._instance = ThreadPoolExecutor(max_workers=10)
             7     return cls._instance
             8  
             9  
            10 class FutureResponseHandler(tornado.web.RequestHandler):
            11   executor = Executor()
            12  
            13   @tornado.web.asynchronous
            14   @tornado.gen.coroutine
            15   def get(self, *args, **kwargs):
            16  
            17     future = Executor().submit(self.ping, 'www.google.com')
            18  
            19     response = yield tornado.gen.with_timeout(datetime.timedelta(10), future,
            20                          quiet_exceptions=tornado.gen.TimeoutError)
            21  
            22     if response:
            23       print 'response', response.result()
            24  
            25   @tornado.concurrent.run_on_executor
            26   def ping(self, url):
            27     os.system("ping -c 1 {}".format(url))
            28     return 'after
            Future:當(dāng)發(fā)送GET請(qǐng)求時(shí),由于方法被@gen.coroutine裝飾且yield 一個(gè) Future對(duì)象,那么Tornado會(huì)等待,等待用戶向future對(duì)象中放置數(shù)據(jù)或者發(fā)送信號(hào),如果獲取到數(shù)據(jù)或信號(hào)之后,就開始執(zhí)行done方法。

            異步非阻塞體現(xiàn)在當(dāng)在Tornaod等待用戶向future對(duì)象中放置數(shù)據(jù)時(shí),還可以處理其他請(qǐng)求。

            注意:在等待用戶向future對(duì)象中放置數(shù)據(jù)或信號(hào)時(shí),此連接是不斷開的。

             1 import tornado.ioloop
             2 import tornado.web
             3 from tornado import gen
             4 from tornado.concurrent import Future
             5 
             6 future = None
             7 class MainHandler(tornado.web.RequestHandler):
             8     @gen.coroutine
             9     def get(self):
            10         global future
            11         future = Future()
            12         future.add_done_callback(self.done)
            13         yield future
            14 
            15     def done(self, *args, **kwargs):
            16         self.write('Main')
            17         self.finish()
            18 
            19 class IndexHandler(tornado.web.RequestHandler):
            20     def get(self):
            21         global future
            22         future.set_result(None)
            23         self.write("Index")
            24 
            25 application = tornado.web.Application([
            26     (r"/main", MainHandler),
            27     (r"/index", IndexHandler),
            28 ])
            29 
            30 if __name__ == "__main__":
            31     application.listen(8888)
            32     tornado.ioloop.IOLoop.instance().start()  #啟動(dòng) IOLoop 的實(shí)例,啟動(dòng)事件循環(huán)機(jī)制,配合非阻塞的 HTTP Server 工作

            posted on 2019-07-14 15:22 Benjamin 閱讀(542) 評(píng)論(0)  編輯 收藏 引用 所屬分類: python

            亚洲欧洲精品成人久久曰影片 | 久久精品国产久精国产| 91精品国产91久久久久福利| 久久夜色精品国产亚洲| 久久夜色精品国产噜噜亚洲a| 囯产精品久久久久久久久蜜桃| 久久精品亚洲日本波多野结衣| 国产精品热久久毛片| 亚洲精品乱码久久久久久自慰| 青青国产成人久久91网| 欧美亚洲国产精品久久高清| 狠狠色丁香婷婷综合久久来| 人妻无码精品久久亚瑟影视| 97超级碰碰碰碰久久久久| 久久亚洲日韩看片无码| 精品久久久久久无码中文野结衣| 日韩人妻无码精品久久久不卡| 精品久久综合1区2区3区激情 | 久久免费香蕉视频| 国产精品免费看久久久| 欧美激情一区二区久久久| 久久91精品综合国产首页| 91久久婷婷国产综合精品青草| 国产成人综合久久精品红| 久久综合九色综合久99| 国产亚洲精午夜久久久久久| 国内精品久久久久影院优| 亚洲va久久久噜噜噜久久| 久久久www免费人成精品| 久久精品国产99国产精品导航| 国产午夜精品久久久久九九| 一级做a爰片久久毛片16| 大伊人青草狠狠久久| 久久亚洲AV成人无码国产| 色综合久久中文字幕无码| 亚洲精品国精品久久99热一| 精品久久人人爽天天玩人人妻| 99蜜桃臀久久久欧美精品网站| 97久久国产综合精品女不卡| 7777久久久国产精品消防器材| 色婷婷综合久久久久中文 |