如何在python应用程序中检测我的系统何时通过DBus或类似方式从挂起状态唤醒?

在后台Python脚本中我需要检测,当系统刚从挂起中醒来时。 什么是不依赖于根脚本而是依赖于诸如DBus等python模块的好方法?

我是dbus的新手,所以我真的可以使用一些示例代码。 从我读到的内容来看

org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Resuming 

任何人都可以帮我解决一些将恢复信号连接到回调的代码吗?

这是一些回答我的问题的示例代码:

 #!/usr/bin/python # This is some example code on howto use dbus to detect when the system returns #+from hibernation or suspend. import dbus # for dbus communication (obviously) import gobject # main loop from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop def handle_resume_callback(): print "System just resumed from hibernate or suspend" DBusGMainLoop(set_as_default=True) # integrate into main loob bus = dbus.SystemBus() # connect to dbus system wide bus.add_signal_receiver( # defince the signal to listen to handle_resume_callback, # name of callback function 'Resuming', # singal name 'org.freedesktop.UPower', # interface 'org.freedesktop.UPower' # bus name ) loop = gobject.MainLoop() # define mainloop loop.run() # run main loop 

请参阅dbus-python教程 。

login1接口现在提供信号。 这是修改后的代码:

 #!/usr/bin/python # slightly different code for handling suspend resume # using login1 interface signals # import dbus # for dbus communication (obviously) import gobject # main loop from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop def handle_sleep_callback(sleeping): if sleeping: print "System going to hibernate or sleep" else: print "System just resumed from hibernate or suspend" DBusGMainLoop(set_as_default=True) # integrate into main loob bus = dbus.SystemBus() # connect to dbus system wide bus.add_signal_receiver( # defince the signal to listen to handle_sleep_callback, # name of callback function 'PrepareForSleep', # signal name 'org.freedesktop.login1.Manager', # interface 'org.freedesktop.login1' # bus name ) loop = gobject.MainLoop() # define mainloop loop.run() # run main loop