Detecting computer/program shutdown in Python?

前端 未结 2 1124
南旧
南旧 2020-11-29 10:12

I have a Python script that runs in a loop regularly making adjustments to my lighting system. When I shut down my computer, I\'d like my script to detect that, and turn of

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 10:43

    You should react to the WM_ENDSESSION message.

    This message is sent when the user logs off or the computer gets shut down.

    If you want to react to Sleep/Hibernate as well, you'll need to handle WM_POWERBROADCAST with PBT_APMSUSPEND.

    But I don't know how to do that in python. I guess it depends on your windowing framework since you need have a windows/a message loop to receive messages.

提交回复
热议问题