Happy New Year everyone! As we know, in the asyncio framework, the main loop runs in one thread. When we call non-blocking tasks that require some time to execute (for example, a query to the database), the main loop switches to other tasks (if any). So, do I understand correctly that at the OS level, "deferred" tasks are branched off into a separate stream? If not, then how are they NOT blocking the main loop if they are in the same thread with it while waiting for the result?

  • Not waiting. They write something like "if there is data on such a socket in the event loop - call me, please," and give control so that other tasks can work. When data appears in such a socket, the event loop will start this task again, and it will work again until the next wait for the socket or until completion - andreymal
  • With the help of non-blocking sockets, the select module and the yield (yield from) operator, you can write your own simplified analogue of asyncio for practice - andreymal
  • Thanks, now it became clear) - violetapple

0