3

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?

Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
  • What exceptions does the application throw when “windows” shuts down? – James Waldby - jwpat7 Dec 17 '12 at 13:21
  • It is throwing exceptions to do with pipes not being writable, and io errors. Basically the application is multiprocessed and I think each of the processes is getting the shutdown command at a different time so I need to handle the shutdown command so I can do some graceful termination. – Matt Seymour Dec 17 '12 at 13:23
  • checkout python's `subprocess` module –  Dec 17 '12 at 14:16
  • Possible duplicate of [Python - Windows Shutdown Events](https://stackoverflow.com/questions/1411186/python-windows-shutdown-events) – handle Apr 15 '18 at 12:58

1 Answers1

5

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.

Community
  • 1
  • 1
James Waldby - jwpat7
  • 8,593
  • 2
  • 22
  • 37