DatascienceGeeks
DatascienceGeeks

Reputation: 438

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?

Upvotes: 4

Views: 592

Answers (1)

Vincent
Vincent

Reputation: 13425

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.

Upvotes: 3

Related Questions