在协程中总会使用到同步IO
来源:11-5 通过aiofiles保存图片文件-2
微微视界123
2019-07-31
老师,请问一下,我在项目中使用的FastDFS实现的文件上传和下载,这样就没法使用异步了,有什么方案可以解决么?
写回答
2回答
-
bobby
2019-08-01
import time from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor MAX_WORKERS = 4 class Handler(tornado.web.RequestHandler): executor = ThreadPoolExecutor(max_workers=MAX_WORKERS) @run_on_executor def background_task(self, i): """ This will be executed in `executor` pool. """ time.sleep(10) return i @tornado.gen.coroutine def get(self, idx): """ Request that asynchronously calls background task. """ res = yield self.background_task(idx) self.write(res)
022019-08-05 -
bobby
2019-07-31
tornado启动的时候有线程池 https://gist.github.com/methane/2185380 可以参照这个将阻塞任务扔到线程池中去运行
012019-08-01
相似问题