[英]Create DBus signal dynamically in python
我已經閱讀了一些與動態創建python方法相關的主題,並且按照它們的說明進行操作,但是它不起作用。 我不知道是否是因為我使用了裝飾器@
或其他東西。
代碼在這里,非常簡單。
運行此代碼時,未發生任何錯誤,但是當我使用D-feet
(檢查dbus信息的工具)時,找不到我創建的新信號。
#!/usr/bin/python
import dbus
import dbus.service
import dbus.glib
import gobject
from dbus.mainloop.glib import DBusGMainLoop
import psutil
class EventServer(dbus.service.Object):
i = 0
@dbus.service.signal('com.github.bxshi.event')
def singal_example(self,msg):
""" example of singals
"""
print msg
def __init__(self):
bus_name = dbus.service.BusName('com.github.bxshi.event', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/com/github/bxshi/event')
def create(self):
self.i +=1
setattr(self.__class__, 'signal_'+str(self.i), self.singal_example)
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
eventserver = EventServer()
gobject.timeout_add(1000,eventserver.create)
loop = gobject.MainLoop()
loop.run()
singal_example
而不是signal_example
create
-方法調用setattr
的類。 我不知道您要做什么,但是您應該發出信號 這是固定的示例:
#!/usr/bin/python
import dbus
import dbus.service
import dbus.glib
import gobject
from dbus.mainloop.glib import DBusGMainLoop
#import psutil
class EventServer(dbus.service.Object):
i = 0
@dbus.service.signal('com.github.bxshi.event')
def signal_example(self,msg):
""" example of singals
"""
print msg
def __init__(self):
bus_name = dbus.service.BusName('com.github.bxshi.event', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/com/github/bxshi/event')
def create(self):
self.i +=1
#setattr(self.__class__, 'signal_'+str(self.i), self.singal_example)
self.signal_example('msg: %d' % self.i)
if __name__ == "__main__":
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
eventserver = EventServer()
gobject.timeout_add(1000,eventserver.create)
loop = gobject.MainLoop()
loop.run()
之后,您可以連接到信號:
# ...
bus = dbus.Bus()
service=bus.get_object('com.github.bxshi.event', '/com/github/bxshi/event')
service.connect_to_signal("signal_example", listener)
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.