tornado中的异步非阻塞



  • tornado是单进程单线程的,往往网络请求当中涉及IO耗时的操作,都会阻塞其他请求,这与美其名曰的“c10k”,相差甚远。其实tornado这种讲究精简极致的框架,是需要开发者充分发挥它的潜能的
    直接上代码:异步非阻塞

    # -*- coding:utf-8 -*-
    import traceback
    
    import time
    import tornado
    from concurrent.futures import ThreadPoolExecutor
    from tornado import web, concurrent
    from tornado.concurrent import run_on_executor
    from tornado.ioloop import IOLoop
    
    
    class Async():
        def __init__(self):
            self.io_loop = IOLoop.current()
            self.executor = ThreadPoolExecutor(max_workers=10)
    
        @run_on_executor
        def cmd(self,func,*args,**kwargs):
            return func(args,**kwargs)
    
    async=Async()
    
    
    class BaseHandler(web.RequestHandler):
        @tornado.web.asynchronous
        @tornado.gen.coroutine
        def get(self,*args):
            try:
                yield async.cmd(self._get,*args)
            except Exception:
                print traceback.format_exc()
                self.finish('network error')
    
    class ReqHandler(BaseHandler):
        def _get(self,*args):
            print 'wait for moment.'
            '''阻塞喽'''
            time.sleep(3)
            print 'rec http1.'
            self.finish('copy that1.')
    
    class ReqHandler2(BaseHandler):
        def _get(self,*args):
            print 'rec http2.'
            self.finish('copy that2.')
    
    def run_main():
        app = tornado.web.Application(handlers=[
            (r'/get',ReqHandler),
            (r'/get2',ReqHandler2),
        ])
        app.listen(9999)
        tornado.ioloop.IOLoop.current().start()
    
    if __name__ == "__main__":
        with concurrent.futures.ProcessPoolExecutor(max_workers=None) as executor:
            '''启动主程序'''
            executor.submit(run_main)
    
    
    
    
    


  • @裸奔的de饺子
    再告诉你一个好玩的东西,可以将所有的IO都变成异步的。
    使用gevent的“猴子补丁”
    pip install gevent

    # 在代码头部加入代码
    from gevent import monkey
    monkey.patch_all()
    

Log in to reply