threads and sys.exit()

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

    threads and sys.exit()

    calling sys.exit() from a thread does nothing... the thread dies, but
    the interpreter remains. i guess the interpreter just catches and
    ignore the SystemExit exception...

    does anybody know of a way to overcome this limitation?


    -tomer

  • Diez B. Roggisch

    #2
    Re: threads and sys.exit()

    gangesmaster wrote:
    [color=blue]
    > calling sys.exit() from a thread does nothing... the thread dies, but
    > the interpreter remains. i guess the interpreter just catches and
    > ignore the SystemExit exception...
    >
    > does anybody know of a way to overcome this limitation?[/color]

    Use Thread.setDaemo n(True) on your threads.

    diez

    Comment

    • gangesmaster

      #3
      Re: threads and sys.exit()

      >>> import threading[color=blue][color=green][color=darkred]
      >>> t=threading.Thr ead(target=sys. exit)
      >>> t.setDaemon(Tru e)
      >>> t.start()
      >>>[/color][/color][/color]

      ?

      Comment

      • gangesmaster

        #4
        Re: threads and sys.exit()

        (i forgot to say it didn't work)

        Comment

        • Rene Pijlman

          #5
          Re: threads and sys.exit()

          gangesmaster:[color=blue]
          >(i forgot to say it didn't work)[/color]

          It's the remaining threads that need to be daemon, when some thread
          performs sys.exit. When no non-daemon thread remains, the application
          terminates.

          --
          René Pijlman

          Comment

          • robert

            #6
            Re: threads and sys.exit()

            gangesmaster wrote:
            [color=blue]
            > calling sys.exit() from a thread does nothing... the thread dies, but
            > the interpreter remains. i guess the interpreter just catches and
            > ignore the SystemExit exception...
            >
            > does anybody know of a way to overcome this limitation?
            >[/color]


            call thread.interrup t_main()

            on *NIX: os.kill(os.getp id(),signal.XXX X)

            or best design your threading correctly with resonable/flexible
            communication channels, e.g. CallQueue:


            (infact that latter (example2) uses sys.exit/SystemExit the other way
            correctly: to terminate a thread cleanly in functional style. in the
            same style reverse the mainthread could be terminated cleanly.)

            I'd

            -robert

            Comment

            • gangesmaster

              #7
              Re: threads and sys.exit()

              i can't make the main thread daemonic. the situation is this:
              * the main thread starts a thread
              * the new thread does sys.exit()
              * the new thread dies, but the process remains
              i can do os.kill(os.getp id()), or TerminateProces s(-1) but that's not
              what i want


              -tomer

              Comment

              • gangesmaster

                #8
                Re: threads and sys.exit()

                that's not a question of design. i just want a child-thread to kill the
                process. in a platform agnostic way.

                Comment

                • robert

                  #9
                  Re: threads and sys.exit()

                  gangesmaster wrote:
                  [color=blue]
                  > that's not a question of design. i just want a child-thread to kill the
                  > process. in a platform agnostic way.
                  >[/color]

                  the clean kill is the 'KeyboardInterr upt'. your code finalizations are
                  at least done corretly. thats also raised on SIGINT by default.

                  so thread.interrup t_main() is that thing to go for on this level - lets
                  say on mid level.

                  killing hard (SIGKILL etc ) is low level. it should be OS-specific, as
                  Python should not make itself inconsistent

                  a good consistent killing on high level can only be done by inter-thread
                  communication - as shown in this recipe above. even if a
                  KeyboardInterru pt is thrown, like with ..

                  #raises SystemExit inside my_thread
                  cq.call(sys.exi t, wait=1, raise_exception =2)

                  ... its thrown exactly app-deterministic a callqueue.recei ve() time.

                  so you have all levels and tools available. If you really miss
                  something, then I'd still say, the design question should be raised.

                  -robert

                  PS: SystemExit / sys.exit (doc: "Exit from Python"!?) is
                  misleading/historic. Its in fact almost "ThreadExit ".

                  Comment

                  • robert

                    #10
                    Re: threads and sys.exit()

                    robert wrote:
                    [color=blue]
                    > killing hard (SIGKILL etc ) is low level. it should be OS-specific, as
                    > Python should not make itself inconsistent[/color]

                    (and os._exit() is that at mid-low level ; better not to say; no
                    finalizations etc.)

                    -robert

                    Comment

                    Working...