'python `asyncio` event loops: how to integrate other, foreign loops?

At the moment I'm struggling a bit with Python asyncio, and with event loops in general. It's probably a rather uncommon experiment, though: I'm trying if I could implement my own event loop (i.e. subclassing asyncio.AbstractEventLoop or similar) which allows me to 'plug' other main loops inside it (e.g. the native main loop of GTK/GLib or another UI toolkit).

I could then instantiate and run my loop, and use it as usual, e.g. using async/await syntax. Beyond that, I can add and remove "other main loops" to it, and it would process this ones as well. For example, I could add the GLib loop to it, so I can use async functions in my GTK project. Maybe even other ones alongside.

So, this surely needs some glue code for each kind of "other loop", which implements an interface that I have to define, and takes care of processing that particular loop when I add it to "my loop". I want this interface to be versatile, i.e. it should be possible to not only use GLib's loop, but all kinds of other ones.

I'm not sure what that interface should indeed be, and how it interacts with my loop implementation. Is there a common pattern or idea how to integrate main loops, which works for GLib and a lot of other ones?

It should also be resource efficient. Otherwise I could just have a while True loop inside run_forever which constantly checks for tasks to execute (and executes them), and constantly calls a particular method of my "other loop" interface, say ForeignLoop.process(self), which could then e.g. call gtk_loop.get_context().iteration(False) for GTK. This would keep one CPU core constantly busy.

So my questions are: Are there better ways to implement my loop idea? Do you think it is possible (without an insane bunch of code, which maybe is even hard to maintain)?

There are already some projects out there which have at least GLib loop integration: There is https://github.com/beeware/gbulb and https://github.com/python-trio/trio, but it will take me ages to understand what they do, since they do a lot of other things as well. There is also https://github.com/jhenstridge/asyncio-glib. That's much more compact and looks interesting. Unfortunately, I don't understand this one as well so far. It does some things that I cannot find much documentation about. What is its basic mechanism? It looks like it works with UNIX' select (as the whole default event loop impl does), but how is that wired to GLib's main loop? And, is that a common approach or a very GTK specific trick?

It turned out to be important to note: My question is not if the idea itself is useful or not. Unless there is a very significant reason to consider it as not useful, at least. :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source