Is asyncio loop.run_in_executor thread safe?

I am trying to use asyncio and should mix it with some standard multi-threaded block code, so I need to offload the execution using run_in_exector.

asyncio docs warn that โ€œmost functionsโ€ are not thread safe , and that call_soon_threadsafe is the only thread protected function. There are a couple others, such as Future.add_done_callback , which are explicitly documented as thread safe. He then has a sentence that says: "you can use run_in_executor to make callbacks in other threads," but do not comment on its safety.

run_in_executor is not doc'd for thread safety, but looking at the source, it looks like it is thread safe if the correct code paths are accepted.

Does anyone know if it should be thread safe, but just not documented that way?

+6
source share
3 answers

run_in_executor should be unsafe by specification (sorry, it looks like an implicit statement and probably should be specified in PEP-3156).

Even if a particular implementation is thread safe, do not allow any implementation compatible with PEP-3156 to be thread safe.

+4
source

I think it completely depends on what you give him. It effectively starts a thread and runs your code, so whether it is thread safe or not depends on what you tell it.

+2
source

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

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

+2
source

Source: https://habr.com/ru/post/979807/


All Articles