install Ctrl-C handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uwe Mayer

    install Ctrl-C handler

    Hi,

    can anybody tell me how you install something like a Ctrl-C handler in
    Python, i.e. a routine that is called when ^C is pressed and that can do
    cleanup work?

    Thanks
    Uwe
  • James Henderson

    #2
    Re: install Ctrl-C handler

    On Wednesday 04 February 2004 12:04 pm, Uwe Mayer wrote:[color=blue]
    > Hi,
    >
    > can anybody tell me how you install something like a Ctrl-C handler in
    > Python, i.e. a routine that is called when ^C is pressed and that can do
    > cleanup work?
    >
    > Thanks
    > Uwe[/color]

    Maybe you require something more sophisticated but ctrl-C raises an exception
    that can be caught:
    [color=blue][color=green][color=darkred]
    >>> try:[/color][/color][/color]
    .... while 1: pass
    .... except KeyboardInterru pt:
    .... print 'Interrupted!'
    ....
    Interrupted!


    James
    --
    James Henderson, Logical Progression Ltd.

    Download MailManager for free. MailManager is designed to solve the problems companies have as the volumes of email they receive increase such as making sure email goes to the right person, making sure it is answered on time, ensuring information in email boxes is shared within th



    Comment

    • James Henderson

      #3
      Re: install Ctrl-C handler

      On Wednesday 04 February 2004 12:15 pm, James Henderson wrote:[color=blue]
      > On Wednesday 04 February 2004 12:04 pm, Uwe Mayer wrote:[color=green]
      > > Hi,
      > >
      > > can anybody tell me how you install something like a Ctrl-C handler in
      > > Python, i.e. a routine that is called when ^C is pressed and that can do
      > > cleanup work?
      > >
      > > Thanks
      > > Uwe[/color]
      >
      > Maybe you require something more sophisticated but ctrl-C raises an
      > exception
      >
      > that can be caught:[color=green][color=darkred]
      > >>> try:[/color][/color]
      >
      > ... while 1: pass
      > ... except KeyboardInterru pt:
      > ... print 'Interrupted!'
      > ...
      > Interrupted![/color]

      Thinking about it, try-finally would also work:
      [color=blue][color=green][color=darkred]
      >>> try:[/color][/color][/color]
      .... while True: pass
      .... finally:
      .... print 'cleaning up'
      ....
      cleaning up
      Traceback (most recent call last):
      File "<stdin>", line 2, in ?
      KeyboardInterru pt


      J.
      --
      James Henderson, Logical Progression Ltd.

      Download MailManager for free. MailManager is designed to solve the problems companies have as the volumes of email they receive increase such as making sure email goes to the right person, making sure it is answered on time, ensuring information in email boxes is shared within th



      Comment

      • Diez B. Roggisch

        #4
        Re: install Ctrl-C handler

        > can anybody tell me how you install something like a Ctrl-C handler in[color=blue]
        > Python, i.e. a routine that is called when ^C is pressed and that can do
        > cleanup work?[/color]

        On *nix you can use the signal-module for general signal handling. There are
        some caveats in multithreaded apps.

        Regards,

        Diez

        Comment

        Working...