Control-C alternative in Windows

Kleine Aap een-niet-bestaande-kleineaap at xs4all.nl
Sun Dec 17 07:49:33 EST 2006


Vlad Dogaru wrote:

> I've written a simple, standalone wiki server in Python. It runs a
> BaseHTTPServer's serve_forever() method until a KeyboardInterrupt is
> caught, at which point it writes changes to a file and exits. This
> works as expected in Linux. However, in Windows I cannot stop the
> script with Control-C. I've only been able to stop it with Ctrl-Break,
> which does not send KeyboardInterrupt. This means no saving to the file
> and effectively a useless script. Any ideas as to how I might make this
> work in Windows?

(http://www.python.org/download/releases/2.2.2/NEWS.txt):

The signal module now supports SIGBREAK on Windows, thanks to Steven
Scott.  Note that SIGBREAK is unique to Windows.  The default SIGBREAK
action remains to call Win32 ExitProcess().  This can be changed via
signal.signal().  For example:

# Make Ctrl+Break raise KeyboardInterrupt, like Python's default Ctrl+C
# (SIGINT) behavior.
import signal
signal.signal(signal.SIGBREAK,
              signal.default_int_handler)

try:
    while 1:
        pass
except KeyboardInterrupt:
    # We get here on Ctrl+C or Ctrl+Break now; if we had not changed
    # SIGBREAK, only on Ctrl+C (and Ctrl+Break would terminate the
    # program without the possibility for any Python-level cleanup).
    print "Clean exit"



More information about the Python-list mailing list