Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python handling system shutdown

I have a multiprocessed python application which is being run as an EXE on windows. Upon selecting to shutdown the operating system the applications throws a number of exceptions as a result of the processes being shutdown.

Is there a way to capture the system shutdown request by windows so I may handle the closure of the multiprocesses myself?

like image 506
Matt Seymour Avatar asked Dec 18 '25 14:12

Matt Seymour


1 Answers

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.

like image 168
James Waldby - jwpat7 Avatar answered Dec 20 '25 04:12

James Waldby - jwpat7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!