我是pythontornado的新手,一直在尝试启动一个新线程来运行一些IO阻塞代码,同时允许服务器继续处理新的请求。我一直在读一些书,但似乎仍然不明白这两个函数之间的区别是什么?你知道吗
例如,调用如下方法:
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\u gresb\u工作簿从对象报告中获取一些信息并将其写入excel电子表格(但是我使用的是openpyxl,它需要大约20秒来加载格式正确的工作簿,另外需要大约20秒来保存它,这会阻止服务器处理新请求!)你知道吗
函数只返回True或False(这就是my_success
的含义),因为报表对象附带了输出文件的路径。你知道吗
我还没有完全得到这两种方法的工作,所以他们可能是不正确的,但只是寻找一些背景资料。你知道吗
干杯!你知道吗
IOLoop.run_in_executor
和Executor.submit
基本上做相同的事情,但返回不同的类型。IOLoop.run_in_executor
返回asyncio.Future
,而Executor.submit
返回concurrent.futures.Future
。你知道吗这两种
Future
类型具有几乎相同的接口,但有一个重要的区别:在协同例程中只有asyncio.Future
可以与await
一起使用。run_in_executor
的目的是提供这种转换。你知道吗相关问题 更多 >
编程相关推荐