之间的差异IOLoop.current当前()。在\u executor()和ThreadPoolExecutor()中运行\u。提交()

2024-10-02 00:36:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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的含义),因为报表对象附带了输出文件的路径。你知道吗

我还没有完全得到这两种方法的工作,所以他们可能是不正确的,但只是寻找一些背景资料。你知道吗

干杯!你知道吗


Tags: 方法函数fromimport服务器myaswith
1条回答
网友
1楼 · 发布于 2024-10-02 00:36:02

IOLoop.run_in_executorExecutor.submit基本上做相同的事情,但返回不同的类型。IOLoop.run_in_executor返回asyncio.Future,而Executor.submit返回concurrent.futures.Future。你知道吗

这两种Future类型具有几乎相同的接口,但有一个重要的区别:在协同例程中只有asyncio.Future可以与await一起使用。run_in_executor的目的是提供这种转换。你知道吗

相关问题 更多 >

    热门问题