是asyncio loop.run_in_executor线程安全?

时间:2021-04-08 21:01:23

I'm trying out asyncio and have to mix it with some normal multi threaded blocking code, so I need to offload the execution using run_in_exector.

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

The asyncio docs warn that "most functions" aren't threadsafe, and that call_soon_threadsafe is the only thread-safe function. There are a couple others, like Future.add_done_callback, too, that are explicitly documented as thread safe. It then has a sentence afterwards saying "you can use run_in_executor to run callbacks in other threads", but doesn't comment on the thread-safety of it specifically.

asyncio文档警告说“大多数函数”不是线程安全的,并且call_soon_threadsafe是唯一的线程安全函数。还有一些其他的,比如未来。add_done_callback也被显式地记录为线程安全。然后,它有一个句子说“您可以使用run_in_executor在其他线程中运行回调”,但是没有具体地评论它的线程安全性。

run_in_executor isn't doc'd to be thread-safe, but looking at the source, it looks like it is thread safe if the right code-paths are taken.

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

Does anyone know if it is supposed to be thread safe, but just isn't documented to be that way?

有人知道它是否应该是线程安全的,但是没有文档证明它是这样的吗?

3 个解决方案

#1


7  

run_in_executor is supposed to be not threadsafe by specification (sorry, it looks like implicit statement and probably should be clarified in PEP-3156).

根据规范,run_in_executor应该不是线程安全的(抱歉,它看起来像是隐式语句,可能应该在pe -3156中进行澄清)。

Even if concrete implementation is thread safe please don't assume that any PEP-3156 compliant implementation will be thread-safe too.

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

#2


3  

The default implementation looks to be thread-safe if the executor parameter is not None or the default executor is already set (call loop.set_default_executor()). Otherwise, two executor may be created.

如果executor参数不是None,或者已经设置了默认的executor(调用loop.set_default_executor()),那么默认实现看起来是线程安全的。否则,可能会创建两个执行程序。

You may write a patch to make the method thread-safe ;-)

您可以编写一个补丁以使方法是线程安全的;

#3


2  

I think it entirely depends on what you give it. It effectively just starts a thread and runs your code, so whether that is thread safe or not depends on what you tell it to do.

我认为这完全取决于你给的是什么。它有效地启动一个线程并运行您的代码,因此这是否线程安全取决于您告诉它做什么。

#1


7  

run_in_executor is supposed to be not threadsafe by specification (sorry, it looks like implicit statement and probably should be clarified in PEP-3156).

根据规范,run_in_executor应该不是线程安全的(抱歉,它看起来像是隐式语句,可能应该在pe -3156中进行澄清)。

Even if concrete implementation is thread safe please don't assume that any PEP-3156 compliant implementation will be thread-safe too.

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

#2


3  

The default implementation looks to be thread-safe if the executor parameter is not None or the default executor is already set (call loop.set_default_executor()). Otherwise, two executor may be created.

如果executor参数不是None,或者已经设置了默认的executor(调用loop.set_default_executor()),那么默认实现看起来是线程安全的。否则,可能会创建两个执行程序。

You may write a patch to make the method thread-safe ;-)

您可以编写一个补丁以使方法是线程安全的;

#3


2  

I think it entirely depends on what you give it. It effectively just starts a thread and runs your code, so whether that is thread safe or not depends on what you tell it to do.

我认为这完全取决于你给的是什么。它有效地启动一个线程并运行您的代码,因此这是否线程安全取决于您告诉它做什么。