Reputation: 14882
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.
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.
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.
Does anyone know if it is supposed to be thread safe, but just isn't documented to be that way?
Upvotes: 12
Views: 3542
Reputation: 1404
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.
You may write a patch to make the method thread-safe ;-)
Upvotes: 3
Reputation: 17366
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).
Even if concrete implementation is thread safe please don't assume that any PEP-3156 compliant implementation will be thread-safe too.
Upvotes: 7
Reputation: 8421
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.
Upvotes: 3