*My answers are assuming you use the default loops provided by Python based on your OS with CPython.
The asyncio.create_task
method is used to schedule the execution of a coroutine (here some_fn
) on the event loop. Since the event loop runs on a single thread, the coroutine will be executed asynchronously to the current context by using cooperative multitasking.
Contrary to that, loop.run_in_executor(None, some_fn)
, will use the ThreadPoolExecutor
to create a thread pool, and perform the tasks, usually BlockingIO, with preemptive multitasking. You can see here the implementation for ThreadPoolExecutor
in CPython. In the implementation, you'll see that it creates a queue and a pool of threads which will get tasks from the queue and perform them. So yes - it will create a new thread for your scheduled task.
A couple of notes:
- A
Task
is a Future
- Generally speaking, if your code does not have blockingIO methods, and you can leverage asyncio for all your IOs, you will be better just using the event loop, without the need to use an
Executor
explicitly. This will be both more efficient since your code now uses cooperative multitasking, and will simplify execution since you know when your code gives back control to the event loop, thus managing concurrency (e.g. race condition) is either very simple or even non-existent.