Python handling system shutdown

夙愿已清 提交于 2019-12-01 08:36:00

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13914495/python-handling-system-shutdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!