在线程内调用dbus-python



在线程内调用dbus方法时,会出现segfault。这是我的场景:我有一个程序Service1,它公开了一个方法测试。第二个程序Service2公开了一个方法公开。由于这种方法会进行一些严肃的数值计算,所以我会将一些参数从暴露传递给正在运行的线程读取器。反过来,这个线程在结束其工作时调用Service1的方法测试。我在最后一次数据库总线调用中得到了segfault。

代码:

# 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()

要进行测试,请运行Service1.py、Service2.py和稍后的代码段:

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在运行此代码几次后应该会崩溃。但为什么呢?

gobject.threads_init()是不够的,您需要调用dbus.mainloop.glib.threads_init()来确保dbus glib线程的安全。

Service1.py中,在将dbus_loop分配给DBusGMainLoop()之前,尝试调用gobject.threads_init()

最新更新