Python处理系统关闭 [英] Python handling system shutdown

查看:114
本文介绍了Python处理系统关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经过多进程处理的python应用程序,正在Windows上以EXE的形式运行.选择关闭操作系统后,由于进程被关闭,应用程序将引发许多异常.

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.

是否可以通过Windows捕获系统关闭请求,以便我自己处理多进程的关闭?

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

推荐答案

A nabble.com 页建议使用win32api.SetConsoleCtrlHandler:

当窗户关闭时,我需要做一些事情,就像有人按下电源按钮一样.我相信这是WM_QUERYENDSESSION或WM_ENDSESSION的窗口消息.我找不到任何将其捕获在python中的方法. atexit()不起作用.使用信号模块捕获SIGBREAK或SIGTERM也不起作用."
您也许可以使用win32api.SetConsoleCtrlHandler并捕获发送到控制台的CTRL_SHUTDOWN_EVENT.

"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.

另请参见 Python Windows关闭事件,其中说:使用win32api.setConsoleCtrlHandler()时,我可以接收关闭信息Windows中的/logoff/etc事件,并彻底关闭我的应用"等.

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.

这篇关于Python处理系统关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆