Control-C alternative in Windows

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

  1. Vlad Dogaru

    Vlad Dogaru Guest

    Hello,

    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
     
    Vlad Dogaru, Dec 17, 2006
    #1
    1. Advertising

  2. Vlad Dogaru

    Kleine Aap Guest

    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"
     
    Kleine Aap, Dec 17, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pberna
    Replies:
    5
    Views:
    3,706
    Scott Allen
    Nov 15, 2004
  2. Guadala Harry

    Alternative to Panel Control

    Guadala Harry, Apr 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    439
    Axel Dahmen
    Apr 29, 2005
  3. Chris Gonnerman
    Replies:
    0
    Views:
    616
    Chris Gonnerman
    Mar 6, 2005
  4. Cameron Walsh
    Replies:
    1
    Views:
    622
  5. Big George
    Replies:
    4
    Views:
    927
    Mahernoz
    Jan 28, 2013
Loading...

Share This Page