Control-C alternative in Windows

Discussion in 'Python' started by Vlad Dogaru, Dec 17, 2006.

  1. Vlad Dogaru

    Vlad Dogaru Guest


    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?

    Thanks in advance,
    Vlad Dogaru, Dec 17, 2006
    1. Advertisements

  2. Vlad Dogaru

    Kleine Aap Guest


    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

    while 1:
    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"
    Kleine Aap, Dec 17, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.