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

con*_*use 6 python programming suspend power-management dbus

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

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

org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Resuming
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决一些将恢复信号连接到回调的代码?

con*_*use 9

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

#!/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
Run Code Online (Sandbox Code Playgroud)

请参阅dbus-python 教程


Nim*_*mar 6

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
Run Code Online (Sandbox Code Playgroud)