gpt4 book ai didi

python - asyncio 的 loop.run_in_executor 是线程安全的吗?

转载 作者:太空狗 更新时间:2023-10-29 20:15:37 25 4
gpt4 key购买 nike

我正在试用 asyncio,并且必须将它与一些普通的多线程阻塞代码混合使用,因此我需要使用 run_in_exector 卸载执行。

asyncio docs warn that "most functions" aren't threadsafe ,并且 call_soon_threadsafe 是唯一的线程安全函数。还有一些其他的,比如 Future.add_done_callback,也被明确记录为线程安全的。然后它后面有一句话说“你可以使用 run_in_executor 在其他线程中运行回调”,但没有具体评论它的线程安全性。

run_in_executor没有文档是线程安全的,但查看源代码,如果采用正确的代码路径,它看起来是线程安全的。

有谁知道它是否应该是线程安全的,但只是没有证明是那样的?

最佳答案

run_in_executor 根据规范 应该是非线程安全的(抱歉,它看起来像是隐式声明,可能应该在 PEP-3156 中澄清).

即使具体实现是线程安全的,也请不要假设任何符合 PEP-3156 的实现也是线程安全的。

关于python - asyncio 的 loop.run_in_executor 是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534471/

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