当在线程内调用dbus方法时,我得到了段错误。这是我的场景:我有一个公开方法测试的程序Service1。第二个程序Service2公开了一个方法expose。当这个方法做一些重要的数值计算时,我将一些参数从expose传递给一个正在运行的线程读取器。该线程在结束其工作时依次调用Service1的方法测试。我在最后一次dbus调用中遇到了段错误。
代码:
# 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应该会在运行这段代码几次后崩溃。但是为什么呢?
发布于 2012-07-20 15:12:19
gobject.threads_init()是不够的,您需要调用dbus.mainloop.glib.threads_init()来确保dbus-glib线程的安全。
发布于 2011-12-21 05:07:14
在Service1.py中,尝试在将dbus_loop分配给DBusGMainLoop()之前调用gobject.threads_init()。
https://stackoverflow.com/questions/6379553
复制相似问题