gpt4 book ai didi

python - 为什么run_in_executor放在BaseEventLoop中?

转载 作者:行者123 更新时间:2023-11-30 23:09:30 25 4
gpt4 key购买 nike

例如,asyncio.gather 具有签名 asyncio.gather(*coros_or_futures, loop=None, return_exceptions=False)

我可以传递特定循环或保留None(并且将使用默认事件循环)。

为什么BaseEventLoop.run_in_executor不以同样的方式定义,例如:asyncio.run_in_executor(executor,callback,*args,loop=None)

是否有一些重要原因将其放入BaseEventLoop

最佳答案

历史上run_in_executor出现得很早,它是一个事件循环的方法。它模仿了twisted 在线程池中运行代码的方法。出现后,run_in_executor 从未改变。它是低级函数,接受回调,并且与接受回调的其他函数非常接近,而不是协程:call_soon()call_later()add_reader( ) 等等,这些都是事件循环的方法。

asyncio.gather 在库开发大约一年之后才被邀请。它位于更高的抽象级别,与协程一起使用,并与其他与协程相关的函数(如 wait()sleep())一起插入。

关于python - 为什么run_in_executor放在BaseEventLoop中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com