Shutdown hook

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

    #1

    Shutdown hook

    Does any one know if python has the ability to run a shutdown hook.

    For example you set a method to run when the python process is shutting
    down, like it recieved a kill signal?

    Basically looking for an effect like the following java code.
    Runtime.getRunt ime().addShutdo wnHook(new Thread(this));

  • Ben Finney

    #2
    Re: Shutdown hook

    Steve <steve.morin@gm ail.com> wrote:[color=blue]
    > Does any one know if python has the ability to run a shutdown hook.[/color]

    When the Python runtime system wants to exit, it raises a SystemExit
    exception.

    Catch that exception at the top level of your code, and do whatever
    you like. (It might be polite to actually exit at some point, of
    course. sys.exit(exitco de) will do so -- raising another SystemExit
    exception.)

    --
    \ "I took it easy today. I just pretty much layed around in my |
    `\ underwear all day. ... Got kicked out of quite a few places, |
    _o__) though." -- Bug-Eyed Earl, _Red Meat_ |
    Ben Finney

    Comment

    • Steve

      #3
      Re: Shutdown hook

      Thanks do appreciate it

      --

      Lisp Programming - You don't know what your missing ...

      =============== =============== ========
      Help Send Laurie to Veterinary School


      Comment

      • Lawrence Oluyede

        #4
        Re: Shutdown hook

        Il 2005-11-15, Ben Finney <bignose+hate s-spam@benfinney. id.au> ha scritto:[color=blue]
        > Steve <steve.morin@gm ail.com> wrote:[color=green]
        >> Does any one know if python has the ability to run a shutdown hook.[/color]
        >
        > When the Python runtime system wants to exit, it raises a SystemExit
        > exception.
        >
        > Catch that exception at the top level of your code, and do whatever
        > you like. (It might be polite to actually exit at some point, of
        > course. sys.exit(exitco de) will do so -- raising another SystemExit
        > exception.)
        >[/color]

        I think using atexit module -
        http://docs.python.org/lib/module-atexit.html is cleaner



        --
        Lawrence - http://www.oluyede.org/blog
        "Anyone can freely use whatever he wants but the light at the end
        of the tunnel for most of his problems is Python"

        Comment

        • Lawrence Oluyede

          #5
          Re: Shutdown hook

          Il 2005-11-15, Ben Finney <bignose+hate s-spam@benfinney. id.au> ha scritto:[color=blue]
          > Steve <steve.morin@gm ail.com> wrote:[color=green]
          >> Does any one know if python has the ability to run a shutdown hook.[/color]
          >
          > When the Python runtime system wants to exit, it raises a SystemExit
          > exception.
          >
          > Catch that exception at the top level of your code, and do whatever
          > you like. (It might be polite to actually exit at some point, of
          > course. sys.exit(exitco de) will do so -- raising another SystemExit
          > exception.)
          >[/color]

          I think using atexit module -
          http://docs.python.org/lib/module-atexit.html is cleaner



          --
          Lawrence - http://www.oluyede.org/blog
          "Anyone can freely use whatever he wants but the light at the end
          of the tunnel for most of his problems is Python"

          Comment

          • Duncan Booth

            #6
            Re: Shutdown hook

            Lawrence Oluyede wrote:
            [color=blue]
            > Il 2005-11-15, Ben Finney <bignose+hate s-spam@benfinney. id.au> ha
            > scritto:[color=green]
            >> Steve <steve.morin@gm ail.com> wrote:[color=darkred]
            >>> Does any one know if python has the ability to run a shutdown hook.[/color]
            >>
            >> When the Python runtime system wants to exit, it raises a SystemExit
            >> exception.
            >>
            >> Catch that exception at the top level of your code, and do whatever
            >> you like. (It might be polite to actually exit at some point, of
            >> course. sys.exit(exitco de) will do so -- raising another SystemExit
            >> exception.)
            >>[/color]
            >
            > I think using atexit module -
            > http://docs.python.org/lib/module-atexit.html is cleaner
            >[/color]
            Correct, although this won't catch all cases where a program is exiting.
            For example, on Windows, if you use Ctrl+C to terminate a program the
            atexit function *is* called, but if you use Ctrl+Break the atexit function
            is not called unless you install a suitable signal handler:

            import signal
            def sigbreak(signum , frame):
            sys.exit("***br eak")

            signal.signal(s ignal.SIGBREAK, sigbreak)

            Even then there are still other ways to terminate a process which will not
            be caught.

            Comment

            • Duncan Booth

              #7
              Re: Shutdown hook

              Lawrence Oluyede wrote:
              [color=blue]
              > Il 2005-11-15, Ben Finney <bignose+hate s-spam@benfinney. id.au> ha
              > scritto:[color=green]
              >> Steve <steve.morin@gm ail.com> wrote:[color=darkred]
              >>> Does any one know if python has the ability to run a shutdown hook.[/color]
              >>
              >> When the Python runtime system wants to exit, it raises a SystemExit
              >> exception.
              >>
              >> Catch that exception at the top level of your code, and do whatever
              >> you like. (It might be polite to actually exit at some point, of
              >> course. sys.exit(exitco de) will do so -- raising another SystemExit
              >> exception.)
              >>[/color]
              >
              > I think using atexit module -
              > http://docs.python.org/lib/module-atexit.html is cleaner
              >[/color]
              Correct, although this won't catch all cases where a program is exiting.
              For example, on Windows, if you use Ctrl+C to terminate a program the
              atexit function *is* called, but if you use Ctrl+Break the atexit function
              is not called unless you install a suitable signal handler:

              import signal
              def sigbreak(signum , frame):
              sys.exit("***br eak")

              signal.signal(s ignal.SIGBREAK, sigbreak)

              Even then there are still other ways to terminate a process which will not
              be caught.

              Comment

              • wangminghua@gmail.com

                #8
                Re: Shutdown hook

                great.
                It's a good idea.

                Comment

                • wangminghua@gmail.com

                  #9
                  Re: Shutdown hook

                  great.
                  It's a good idea.

                  Comment

                  • Steve Juranich

                    #10
                    Re: Shutdown hook

                    On 15 Nov 2005 14:45:27 -0800, Steve <steve.morin@gm ail.com> wrote:[color=blue]
                    > Does any one know if python has the ability to run a shutdown hook.[/color]

                    Look at the "atexit" module.

                    barbet (~)$ pydoc atexit.register
                    Help on function register in atexit:

                    atexit.register = register(func, *targs, **kargs)
                    register a function to be executed upon normal program termination

                    func - function to be called at exit
                    targs - optional arguments to pass to func
                    kargs - optional keyword arguments to pass to func
                    --
                    Steve Juranich
                    Tucson, AZ
                    USA

                    Comment

                    • Steve Juranich

                      #11
                      Re: Shutdown hook

                      On 15 Nov 2005 14:45:27 -0800, Steve <steve.morin@gm ail.com> wrote:[color=blue]
                      > Does any one know if python has the ability to run a shutdown hook.[/color]

                      Look at the "atexit" module.

                      barbet (~)$ pydoc atexit.register
                      Help on function register in atexit:

                      atexit.register = register(func, *targs, **kargs)
                      register a function to be executed upon normal program termination

                      func - function to be called at exit
                      targs - optional arguments to pass to func
                      kargs - optional keyword arguments to pass to func
                      --
                      Steve Juranich
                      Tucson, AZ
                      USA

                      Comment

                      Working...