Calling dbus-python inside a thread

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I'm getting segfaults when calling a dbus method inside a thread. This is my scenario: I have a program Service1 that exposes a method test. A second program Service2 exposes a method expose. As this method does some serious numerical computation, I pass some params from expose to a running thread reader. This thread, in turn, calls the method test of Service1 when it ends its work. I'm geting segfaults in last dbus call.

The code:

# Service1.py class Service1(Object):     def __init__(self, bus):         name = BusName('com.example.Service1', bus)         path = '/'        super(Service1, self).__init__(name, path)      @method(dbus_interface='com.example.Service1',         in_signature='s', out_signature='s')     def test(self, test):         print 'test being called'         return test  dbus_loop = DBusGMainLoop() dsession = SessionBus(mainloop=dbus_loop) loop = gobject.MainLoop() gobject.threads_init()  im = Service1(dsession) loop.run()   # Service2.py dbus_loop = DBusGMainLoop() dsession = SessionBus(mainloop=dbus_loop)  class Service2(Object): def __init__(self, bus):     name = BusName('com.example.Service2', bus)     super(Service2, self).__init__(name, '/')      self.queue = Queue()     self.db = bus.get_object('com.example.Service1', '/')     self.dbi = dbus.Interface(self.db, dbus_interface='com.example.Service1')  @method(dbus_interface='com.example.Service2',         in_signature='', out_signature='') def expose(self):     print 'calling expose'     self.queue.put(('params',))  def reader(self):     while True:         val = self.queue.get()         dd = self.dbi.test('test')         print dd         self.queue.task_done()   gobject.threads_init() loop = gobject.MainLoop()  im = Service2(dsession) reader = threading.Thread(target=im.reader) reader.start()  loop.run() 

To test, run Service1.py, Service2.py and later this snippet:

dbus_loop = DBusGMainLoop() session = SessionBus(mainloop=dbus_loop) proxy = session.get_object('com.example.Service2', '/') test_i = dbus.Interface(proxy, dbus_interface='com.example.Service2') test_i.expose() 

Service2.py should crash after running this code a few times. But why?

回答1:

gobject.threads_init() is not enough, you need to call dbus.mainloop.glib.threads_init() to make dbus-glib thread safe.



回答2:

In Service1.py, Try calling gobject.threads_init() before assigning dbus_loop to DBusGMainLoop().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!