Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is asyncio.run_in_executor multithreading?

The event loop is meant to be thread-specific, since asyncio is about cooperative multitasking using single thread. So I don't understand how asyncio.run_in_exceutor work together with ThreadPoolExcecutor?

like image 202
DatascienceGeeks Avatar asked Aug 31 '25 05:08

DatascienceGeeks


1 Answers

I would like to know the purpose of the function

The loop.run_in_executor awaitable has two main use cases:

  1. Perform an I/O operation that cannot be managed through the file descriptor interface of the selector loop (i.e using the loop.add/remove_reader methods). This happens occasionally, see how the code for loop.getaddrinfo uses loop.run_in_executor under the hood for instance.

  2. Perform a heavy CPU operation that would block the event loop context switching mechanism for too long. There are plenty of legitimate use cases for that, imagine running some data processing task in the context of an asyncio application for instance.

like image 51
Vincent Avatar answered Sep 02 '25 17:09

Vincent