Python IOLoop.current().run_in_executor()与ThreadPoolExecutor().submit()之间的差异
我是PythonTornado的新手,一直在尝试启动一个新线程来运行一些IO阻塞代码,同时允许服务器继续处理新请求。我一直在读一些书,但似乎仍然不明白这两个函数之间的区别是什么 例如,调用如下方法:Python IOLoop.current().run_in_executor()与ThreadPoolExecutor().submit()之间的差异,python,tornado,threadpoolexecutor,Python,Tornado,Threadpoolexecutor,我是PythonTornado的新手,一直在尝试启动一个新线程来运行一些IO阻塞代码,同时允许服务器继续处理新请求。我一直在读一些书,但似乎仍然不明白这两个函数之间的区别是什么 例如,调用如下方法: from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(1) as executor: future = executor.submit(report.write_gresb_workbook)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(1) as executor:
future = executor.submit(report.write_gresb_workbook)
print(future.result())
与之相比:
from concurrent.futures import ThreadPoolExecutor
from tornado import ioloop
with ThreadPoolExecutor(1) as executor:
my_success = await ioloop.IOLoop.current().run_in_executor(executor, report.write_gresb_workbook)
print(my_success)
write_gresb_工作簿从对象报告中获取一些信息,并将其写入excel电子表格(但是我使用openpyxl,它需要~20秒才能加载格式正确的工作簿,另外~20秒才能保存它,这会阻止服务器处理新请求!)
该函数只返回True或False(这就是报告对象附加了输出文件的路径时的结果)
我还没有完全得到这两种方法的工作,所以他们可能是不正确的,但只是寻找一些背景信息
干杯
IOLoop.run_in_executor
和executor.submit执行基本相同的操作,但返回不同的类型IOLoop.run\u in\u executor
返回一个asyncio.Future
,而executor.submit
返回一个concurrent.futures.Future
这两种Future
类型具有几乎相同的接口,但有一个重要的区别:只有asyncio。Future
可以在协同例程中与wait
一起使用。run\u in\u executor
的目的是提供这种转换