2

I'm using pygattlib to interface with Bluetooth device in Python. It works just fine, but the problem occurs when I try to use DBus in my code.

If I import gattlib and GLib.MainLoop().run(), the program freezes when calling run().
I found that gattlib has its own MainLoop for internal async calls.

I didn't find anything that would cover this specific issue, just some examples of having multiple mainloops in C.

This is the relevant part of the Python code:

import gattlib as bt

try:
    from gi.repository import GLib
except ImportError:
    import glib as GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

[...do stuff with gattlib here...]

DBusGMainLoop(set_as_default=True)
dbusService = SystemDBus()
try:
    loop = GLib.MainLoop()
    loop.run() # it stalls here, doesn't respond to DBus or anything
except KeyboardInterrupt:
    GLib.MainLoop().quit()
Blagus
  • 65
  • 1
  • 8
  • Did you get the same behaviour using `DBusGMainLoop(set_as_default=False)` ? You can also check [DBus documentation](https://dbus.freedesktop.org/doc/dbus-python/api/dbus.mainloop.glib-module.html). – Chiheb Nexus Nov 16 '16 at 13:17
  • `RuntimeError: To make asynchronous calls, receive signals or export objects, D-Bus connections must be attached to a main loop by passing mainloop=... to the constructor or calling dbus.set_default_main_loop(...)` It doesn't work. And I can't pass mainloop since pygattlib doesn't expose its internal mainloop. Do you think it would work if I patched pygattlib to expose its own mainloop object? – Blagus Nov 16 '16 at 14:22
  • dbus-python is largely a dead library, the maintainer suggests using GLib's Gio directly which will of course integrate into its own mainloop well. – TingPing Nov 17 '16 at 07:56

0 Answers0