windows使用python结束程序_如何检测Windows上python中退出的程序并在退出时执行某些操作...

I want to do something when terminate the python script on windows.

# coding:utf-8

import ctypes

import os

def set_exit_handler():

def on_exit(event):

print '=====exit====='

_BOOL = ctypes.c_long

_DWORD = ctypes.c_ulong

_kernel32 = ctypes.windll.kernel32

_SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD)

_kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL]

_kernel32.SetConsoleCtrlHandler.restype = _BOOL

h = _SIGNAL_HANDLER(on_exit)

if not _kernel32.SetConsoleCtrlHandler(h, True):

raise ctypes.WinError()

print 'register success'

if __name__ == '__main__':

set_exit_handler()

while(1):

pass

Please check my sample code. It has a problem. when I press CTRL+C or Close the cmd window.on_exit() will not be executed.and windows popup "python.exe has stopped working, windows is checking the solution to the problem "

Thanks in advance and sorry for poor english.

解决方案

As @mata suggests, you should use the atexit module to register a function to be called when the script exits normally, i.e. not via an unhandled Windows exception, ExitProcess, or TerminateProcess.

If you need to use SetConsoleCtrlHandler for some other reason, keep a reference to the callback to prevent it from being garbage collected. Otherwise the process will crash (at best).

import ctypes

from ctypes import wintypes

_kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

def _check_bool(result, func, args):

if not result:

raise ctypes.WinError(ctypes.get_last_error())

# else build final result from result, args, outmask, and

# inoutmask. Typically it's just result, unless you specify

# out/inout parameters in the prototype.

return args

_HandlerRoutine = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)

_kernel32.SetConsoleCtrlHandler.errcheck = _check_bool

_kernel32.SetConsoleCtrlHandler.argtypes = (_HandlerRoutine,

wintypes.BOOL)

_console_ctrl_handlers = {}

def set_console_ctrl_handler(handler):

if handler not in _console_ctrl_handlers:

h = _HandlerRoutine(handler)

_kernel32.SetConsoleCtrlHandler(h, True)

_console_ctrl_handlers[handler] = h

You'd also want an unset_console_ctrl_handler function.

FYI, the console isn't a "cmd" window. cmd.exe is a console user interface (CUI) program that's typically the %COMSPEC% command interpreter. In this respect it's no different from powershell.exe or python.exe, or any other console application.

A console window implements a character interface that's compatible with traditional standard I/O using StandardInput, StandardOutput, and StandardError. There's also a functional API (as opposed to terminal control sequences) to create more elaborate text interfaces. Each UCS-2 character in the console buffer has attributes such as color and intensity.

Prior to NT 6.1, each console is hosted by a thread in the system server process, csrss.exe. In NT 6.1+, each console window is instead hosted in an instance of conhost.exe, which is more secure since it's not a system process and it gives each console window a separate process and security context. Prior to NT 6.3, a process communicates with its (one and only one) attached console via Windows local procedure call (LPC). In NT 6.3+ it instead uses the ConDrv device driver. Multiple processes can share the same console window.

Scripts with the extension .pyw are associated with pythonw.exe, which is built as a windowed application that doesn't inherit or create a console window. If you need a console in this case, you can allocate a new one or attach to an existing one by using ctypes to call AllocConsole or AttachConsole.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值