Tornado で process/thread pool
Tornado-3.0 から gen.coroutine
が Future
を使うようになったので、 concurrent.futures
を使って process/thread pool が簡単に利用できるようになった。
という事の確認。
import concurrent.futures from tornado import gen, web def heavy_process(): import time time.sleep(10) return 42 _executor = None def executor(): global _executor if _executor is None: #_executor = concurrent.futures.ProcessPoolExecutor() _executor = concurrent.futures.ThreadPoolExecutor(4) return _executor class MyHandler(web.RequestHandler): @web.asynchronous @gen.coroutine def get(self): result = yield executor().submit(heavy_process) self.write('{0}\n'.format(result)) application = web.Application([ (r'/', MyHandler), ]) def main(): from tornado import ioloop, log log.enable_pretty_logging() application.listen(8888) ioloop.IOLoop.instance().start() if __name__ == '__main__': main()