2

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 "

Please check the windows api at http://msdn.microsoft.com/en-us/library/windows/desktop/ms685049%28v=vs.85%29.aspx

Thanks in advance and sorry for poor english.

1

2 Answers 2

5

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.

1
  • Thanks for your answear.I resolved my problem by your solution ^_^.
    – scola
    Commented Nov 12, 2013 at 7:07
3

You should use the atexit module, which allows you to register exit hooks without using os specific function calls.

1
  • 1
    Thanks for your advice,I can execute the onexit when press CTRL+C.but if I close the console, the onexit will not be executed.so I apply the solution of @eryksun below.
    – scola
    Commented Nov 12, 2013 at 7:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.