DBussy + GLib Event Loop

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Thu Oct 5 22:42:38 UTC 2017


DBussy <https://github.com/ldo/dbussy>, <https://gitlab.com/ldo/dbussy>
is my Python binding for libdbus that takes advantage of the asyncio
<https://docs.python.org/3/library/asyncio.html> event-loop framework
with the coroutine capability introduced with Python 3.5.

Of course, every GUI toolkit already defines its own event loop. For
example, GTK+ is built around one provided by GLib
<https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html>.
And nobody is going to replace their event loop with Python’s one.

But they don’t need to. asyncio provides a default event loop, but more
than that, it defines a standard event-loop API. And it doesn’t really
require that much work to take an existing event loop and wrap it to be
compatible with this API, as a subclass of asyncio.AbstractEventLoop.
And then a GUI toolkit event loop that knows nothing of coroutines,
originally written for a language that had no such capability, becomes
capable of supporting coroutines in Python.

For example, glibcoro <https://github.com/ldo/glibcoro>,
<https://gitlab.com/ldo/glibcoro> is my attempt at an
asyncio-compatible layer wrapping the GLib event loop. So far it is
about 370 lines of code, which is not a large amount. And it is
functional enough to run these coroutine-intensive examples
<https://github.com/ldo/glibcoro_examples>,
<https://gitlab.com/ldo/glibcoro_examples>, which include a couple
showing off the usefulness of coroutines in a GUI-based app.

A new example I just added to this repo is a version of the
D-Bus signal_listener script from my collection of examples of DBussy in
action. Like the other non-GUI examples, it can run with either event
loop, just by setting an environment variable, to demonstrate that the
behaviour is just about identical either way. (Can you find any
differences?)


More information about the dbus mailing list