ubuntu 12.04 を使用しています。Python でサスペンド イベントをキャッチする方法はありますか? シャットダウンイベントをキャッチするための同じ質問。
3431 次
3 に答える
5
誰かが同じ問題に遭遇した場合、コードは次のとおりです。
#!/usr/bin/env python
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"
def handle_suspend_callback():
print "System about to 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
)
bus.add_signal_receiver( # defince the signal to listen to
handle_suspend_callback, # name of callback function
'Sleeping', # singal name
'org.freedesktop.UPower', # interface
'org.freedesktop.UPower' # bus name
)
loop = gobject.MainLoop() # define mainloop
loop.run() # run main loop
于 2012-11-25T06:57:15.803 に答える
5
最も簡単な方法は、 DBUS pythonインターフェイスを使用し、「org.freedesktop.UPower」インターフェイスで「AboutToSleep」および/または「Sleeping」イベントをリッスンすることだと思います
于 2012-11-23T13:09:53.767 に答える
3
このコードを拡張して、acpid からのイベントをリッスンし、受け取った文字列を出力して、必要なイベントを生成し、文字列がどのように見えるかを確認してみてください。
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/var/run/acpid.socket")
print "Connected to acpid"
while 1:
for event in s.recv(4096).split('\n'):
event=event.split(' ')
if len(event)<2: continue
print event
if event[0]=='ac_adapter':
if event[3]=='00000001': #plugged
plugged() #Power plugged event
else: #unplugged
unplugged() #Power unplugged event
elif event[0]=='button/power':
power_button() #Power button pressed
elif event[0]=='button/lid':
if event[2]=='open':
lid_open() #Laptop lid opened
elif event[2]=='close':
lid_close() #Laptop lid closed
于 2012-11-23T13:42:28.560 に答える