4

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?

2
  • That's precisely the point, run_in_executor lets you run code outside of the event loop (using the executor of your choice). Is your question about the purpose of this function or the technical aspect of it? Commented Sep 22, 2020 at 10:42
  • @Vincent thanks for your comment, I would like to know the purpose of the function Commented Sep 22, 2020 at 11:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.