How can I find out how to implement a custom Python asyncio event loop?

I am looking at an implementation of a new event loop to connect to asynciobased on existing runtime implementations such as Cocoa s NSRunLoopand Qts QEventLoop. but it’s hard to find a place to start.

The documentation says that the system is designed to connect, but nowhere does it say exactly how this can be done. Should I start with AbstractEventLoopor BaseEventLoop? Which method does what and what components do I need to provide? The only alternative implementation that I find useful is uvloop , but it is hard to understand because it relies heavily on Cython and libuv, which I am not familiar with.

Is there any record of how the implementation of the event loop is performed, and how can I create a custom one? Or a less complicated implementation, can I wrap my head faster? Thanks for any pointers.

+4
source share
1 answer

documentation speaks of inheritance from AbstractEventLoop.

For the rest of your question, I did not find the documentation very clear, but the source code for a particular event loop asynciowas useful. I wrote a pretty minimal example of inheritance from AbstractEventLoopto create an event driven simulator.

The main things that I would like to say are

  • create_task. coroutine asyncio.ensure_future(coro()), loop create_task. - , def create_task(self, coro): return asyncio.Task(coro, loop=self).

  • call_soon, call_at call_later. . async/wait, .

  • , loop call_exception_handler. , - , , .

  • AbstractEventLoop, , . : .

+1

Source: https://habr.com/ru/post/1659326/


All Articles