Running an async background task in Tornado

前端 未结 5 1435
北荒
北荒 2020-12-15 06:50

Reading the Tornado documentation, it\'s very clear how to call an async function to return a response:

class GenAsyncHandler(RequestHandler):
    @gen.corou         


        
5条回答
  •  被撕碎了的回忆
    2020-12-15 07:20

    I have a time-consuming task in post request, maybe more than 30 minutes need, but client required return a result immediately.

    First, I used IOLoop.current().spawn_callback. It works! but! If the first request task is running, second request task blocked! Because all tasks are in main event loop when use spawn_callback, so one task is synchronous execution, other tasks blocked.

    Last, I use tornado.concurrent. Example:

    import datetime
    import time
    
    from tornado.ioloop import IOLoop
    import tornado.web
    from tornado import concurrent
    
    executor = concurrent.futures.ThreadPoolExecutor(8)
    
    
    class Handler(tornado.web.RequestHandler):
    
        def get(self):
            def task(arg):
                for i in range(10):
                    time.sleep(1)
                    print(arg, i)
    
            executor.submit(task, datetime.datetime.now())
            self.write('request accepted')
    
    
    def make_app():
        return tornado.web.Application([
            (r"/", Handler),
        ])
    
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(8000, '0.0.0.0')
        IOLoop.current().start()
    

    and visit http://127.0.0.1:8000, you can see it's run ok:

    2017-01-17 22:42:10.983632 0
    2017-01-17 22:42:10.983632 1
    2017-01-17 22:42:10.983632 2
    2017-01-17 22:42:13.710145 0
    2017-01-17 22:42:10.983632 3
    2017-01-17 22:42:13.710145 1
    2017-01-17 22:42:10.983632 4
    2017-01-17 22:42:13.710145 2
    2017-01-17 22:42:10.983632 5
    2017-01-17 22:42:16.694966 0
    2017-01-17 22:42:13.710145 3
    2017-01-17 22:42:10.983632 6
    2017-01-17 22:42:16.694966 1
    2017-01-17 22:42:13.710145 4
    2017-01-17 22:42:10.983632 7
    2017-01-17 22:42:16.694966 2
    2017-01-17 22:42:13.710145 5
    2017-01-17 22:42:10.983632 8
    2017-01-17 22:42:16.694966 3
    2017-01-17 22:42:13.710145 6
    2017-01-17 22:42:19.790646 0
    2017-01-17 22:42:10.983632 9
    2017-01-17 22:42:16.694966 4
    2017-01-17 22:42:13.710145 7
    2017-01-17 22:42:19.790646 1
    2017-01-17 22:42:16.694966 5
    2017-01-17 22:42:13.710145 8
    2017-01-17 22:42:19.790646 2
    2017-01-17 22:42:16.694966 6
    2017-01-17 22:42:13.710145 9
    2017-01-17 22:42:19.790646 3
    2017-01-17 22:42:16.694966 7
    2017-01-17 22:42:19.790646 4
    2017-01-17 22:42:16.694966 8
    2017-01-17 22:42:19.790646 5
    2017-01-17 22:42:16.694966 9
    2017-01-17 22:42:19.790646 6
    2017-01-17 22:42:19.790646 7
    2017-01-17 22:42:19.790646 8
    2017-01-17 22:42:19.790646 9
    

    Want to help everyone!

提交回复
热议问题