这种将线程池与龙卷风一起使用的风格可以吗?

Is this style of using Thread pool with tornado ok?

所以我在 class

中创建了一个名为 executor 的 class 变量
executor = ThreadPoolExecutor(100)

我没有使用函数和方法并使用装饰器,而是简单地使用以下行来处理我的异步方法中的阻塞任务(如 io 和哈希创建以及....)

result = await to_tornado_future(self.executor.submit(blocking_method, param1, param2)

我决定使用这种风格,因为

1- 装饰器本质上比较慢

2-不需要额外的方法和函数

3- 它按预期工作并且在需要之前没有创建线程

我说得对吗?请使用原因(我想知道我使用的方式是否较慢或占用更多资源或....)

更新

根据本的回答,我的上述做法是不正确的 所以我最终根据需要使用了以下功能,我认为这是最好的方法

def pool(pool_executor, fn, *args, **kwargs):
    new_future = Future()
    result_future = pool_executor.submit(fn, *args, **kwargs)
    result_future.add_done_callback(lambda f: new_future.set_result(f.result()))
    return new_future

用法:

result = await pool(self.executor, time.sleep, 3)

只要您的所有阻塞方法都是线程安全的,这就是安全的。既然你提到了在这些线程中执行 IO,我会指出在这里执行文件 IO 是可以的,但是 Tornado 中的所有网络 IO 都必须发生在 IOLoop 的线程上。

为什么说"decorators are slower by nature"?哪些装饰器比什么慢?一些装饰器根本没有性能开销(尽管大多数确实有一些运行时成本)。 to_tornado_future(executor.submit()) 也不是免费的。 (顺便说一句,我认为你想要 tornado.gen.convert_yielded 而不是 tornado.platform.asyncio.to_tornado_futureexecutor.submit 不是 return 和 asyncio.Future)。

作为一般规则,线程池上的 运行 blocking_method 将比直接调用它 。仅当 blocking_method 可能阻塞足够长的时间以至于您希望主线程在此期间有空做其他事情时才应该这样做。