Python - Windows Shutdown Events
The problem here was that the HWND_MESSAGE
window type doesn't actually receive broadcast messages - like the WM_QUERYENDSESSION
and WM_ENDSESSION
.
So instead of specifying win32con.HWND_MESSAGE
for the "parent window" parameter of CreateWindowEx()
, I just specified 0
.
Basically, this creates an actual window, but I never show it, so it's effectively the same thing. Now, I can successfully receive those broadcast messages and shut down the app properly.
Detect shutdown event with python
So, what you will need is the win32API and the function described here. You can use this function to add what's called a Control Handler Method that will run whenever the program is being shut down or terminated for any reason, including shutdown. You can find a list of the different codes that can be passed to the handler and their meanings here. Ideally, you should have a handler method that just shuts down the server, waits for it to finish shutting down, and then return.
I don't have any personal experience with the library, but it should be fairly straightforward.
EDIT: as noted by @ErykSun, you will need to create a hidden window in order to receive the events. To be quite honest I'm not sure how to create that hidden window. Some documentation suggests that running your application as a service may also work. I will look into this more if I get time.
Python handling system shutdown
A nabble.com page suggests using win32api.SetConsoleCtrlHandler
:
“I need to do something when windows shuts down, as when someone presses the power button. I believe this is a window message, WM_QUERYENDSESSION or WM_ENDSESSION. I can't find any way to trap this in python. atexit() does not work. Using the signal module to trap SIGBREAK or SIGTERM does not work either.”
You might be able to use win32api.SetConsoleCtrlHandler and catch the CTRL_SHUTDOWN_EVENT that's sent to the console.
Also see Python windows shutdown events, which says, “When using win32api.setConsoleCtrlHandler() I'm able to receive shutdown/logoff/etc events from Windows, and cleanly shut down my app” etc.
Detecting computer/program shutdown in Python?
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.
trigger a function when pc is shutting down - tkinter
I am not aware of Tkinter having a specific tool to catch system shutdowns like you suggest. You would be better registering a signal handler that can catch the shutdown signal and trigger the in_close
function.
(Also worth looking here for some extra details)
import signal
def in_close(signum = None, frame = None):
# Here you can attempt to gently exit your application.
pass
# For some common signals that will close your program.
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, in_close)
As pointed out in the comments, this is not a Windows valid solution. For discussions on handling windows shutdown events see:
Python - Windows Shutdown Events
Related Topics
How to Manage Third-Party Python Libraries with Google App Engine? (Virtualenv? Pip)
How to Get the "Id" After Insert into MySQL Database with Python
Python Global Exception Handling
Regex Error - Nothing to Repeat
How to Get the Current Time in Milliseconds in Python
How to Decorate All Functions of a Class Without Typing It Over and Over for Each Method
How to Suppress the Newline After a Print Statement
How to Normalize a Numpy Array to a Unit Vector
Sqlalchemy Create_All() Does Not Create Tables
Why Does Defining _Getitem_ on a Class Make It Iterable in Python
Easy Way of Finding Decimal Places
Count Consecutive Occurences of Values Varying in Length in a Numpy Array
Make Requests Using Python Over Tor
Combining Conda Environment.Yml with Pip Requirements.Txt
Python Requests - How to Use System Ca-Certificates (Debian/Ubuntu)