os._exit vs. sys.exit

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

    #1

    os._exit vs. sys.exit

    Quick question:

    Why does os._exit called from a Python Timer kill the whole process while
    sys.exit does not? On Suse.

    Bryan


  • Andrew Dalke

    #2
    Re: os._exit vs. sys.exit

    Bryan wrote:[color=blue]
    > Why does os._exit called from a Python Timer kill the whole process while
    > sys.exit does not? On Suse.[/color]

    os._exit calls the C function _exit() which does an immediate program
    termination. See for example

    and note the statement "can never return".

    sys.exit() is identical to "raise SystemExit()". It raises a Python
    exception which may be caught at a higher level in the program stack.

    Andrew
    dalke@dalkescie ntific.com


    Comment

    • Peter Hansen

      #3
      Re: os._exit vs. sys.exit

      Andrew Dalke wrote:[color=blue]
      > sys.exit() is identical to "raise SystemExit()". It raises a Python
      > exception which may be caught at a higher level in the program stack.[/color]

      And which *is* caught at the highest levels of threading.Threa d objects
      (which Timer is based on). Exceptions raised (and caught or not) in a
      Thread do not have any effect on the main thread, and thus don't affect
      the interpreter as a whole.

      -Peter

      Comment

      • Bryan

        #4
        Re: os._exit vs. sys.exit


        "Peter Hansen" <peter@engcorp. com> wrote in message
        news:u-SdnRAzQ9KHhnffR Vn-gg@powergate.ca ...[color=blue]
        > Andrew Dalke wrote:[color=green]
        >> sys.exit() is identical to "raise SystemExit()". It raises a Python
        >> exception which may be caught at a higher level in the program stack.[/color]
        >
        > And which *is* caught at the highest levels of threading.Threa d objects
        > (which Timer is based on). Exceptions raised (and caught or not) in a
        > Thread do not have any effect on the main thread, and thus don't affect
        > the interpreter as a whole.
        >
        > -Peter[/color]

        Thanks for the clarifications. One more question, can I catch this
        exception in my main thread and then do another sys.exit() to kill the whole
        process?

        Apparently sys.exit() allows the program to clean up resources and exit
        gracefully, while os._exit() is rather abrupt.

        Bryan


        Comment

        • Peter Hansen

          #5
          Re: os._exit vs. sys.exit

          Bryan wrote:[color=blue]
          > Thanks for the clarifications. One more question, can I catch this
          > exception in my main thread and then do another sys.exit() to kill the whole
          > process?[/color]

          Not as such. Exceptions can be caught only in the thread in which they
          are raised. There are tricky techniques to change this, but they would
          have to rely on things which are themselves sufficient for what you are
          trying to do.
          [color=blue]
          > Apparently sys.exit() allows the program to clean up resources and exit
          > gracefully, while os._exit() is rather abrupt.[/color]

          What does the main thread do while the other thread is running? If it's
          just waiting for it and other threads to finish/fail, then you need to
          have some kind of loop that waits for all other threads to not respond
          True to .isAlive(), and/or you need to add a threading.Event or
          something like it which the main thread can wait on or poll to see
          whether a thread has caught an exception, *and* you need to make all
          those other threads catch their own exceptions at the top levels of
          their run() method, and to set that Event object if SystemExit is
          caught. Or related techniques.

          If that's not enough ideas for you to figure something out, please
          provide more detail and we can come up with something more specific and
          appropriate. For example, do you want to exit the app only if a thread
          raises SystemExit, or would other exceptions result in the same effect?

          -Peter

          Comment

          Working...