According to the documentation, when passing None
to run_in_executor
the default executor is used:
awaitable loop.run_in_executor(executor, func, *args) Arrange for func to be called in the specified executor.
The executor argument should be an concurrent.futures.Executor instance. The default executor is used if executor is None.
My question is, what is the default executor of asyncio?
run_in_executor is used to manage threads from within an event loop. To this end, it needs to wrap the thread into a Future, which needs to be assigned to an event loop (in one way or another). The reason the method is stored directly in a loop object is probably historical. It might as well have been asyncio.
run_in_executor() method can be used with a concurrent. futures. ThreadPoolExecutor to execute blocking code in a different OS thread without blocking the OS thread that the event loop runs in.
Run an asyncio Event Loop run_until_complete(<some Future object>) – this function runs a given Future object, usually a coroutine defined by the async / await pattern, until it's complete. run_forever() – this function runs the loop forever. stop() – the stop function stops a running loop.
Run the event loop until stop() is called. If stop() is called before run_forever() is called, the loop will poll the I/O selector once with a timeout of zero, run all callbacks scheduled in response to I/O events (and those that were already scheduled), and then exit.
Runs jobs in the default executor of the event loop. If the job function is a native coroutine function, it is scheduled to be run directly in the event loop as soon as possible. All other functions are run in the event loop’s default executor which is usually a thread pool.
Set executor as the default executor used by run_in_executor () . executor should be an instance of ThreadPoolExecutor. Deprecated since version 3.8: Using an executor that is not an instance of ThreadPoolExecutor is deprecated and will trigger an error in Python 3.9.
So asyncio is not a replacement for all types of asynchronous execution. Asyncio is designed around the concept of ‘cooperative multitasking’, so you have complete control over when a CPU ‘context switch’ occurs (i.e. context switching happens at the application level and not the hardware level).
The executor argument should be an concurrent.futures.Executor instance. The default executor is used if executor is None.
My question is, what is the default executor of asyncio?
It's a concurrent.futures.ThreadPoolExecutor
with default settings. Previously one could also call set_default_executor
to use a different kind of executor, but as of Python 3.8 it's guaranteed to be a ThreadPoolExecutor
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With