470,599 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

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

Apr 24 '06 #1
9 22802
gangesmaster wrote:
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?


Use Thread.setDaemon(True) on your threads.

diez
Apr 24 '06 #2
>>> import threading
t=threading.Thread(target=sys.exit)
t.setDaemon(True)
t.start()


?

Apr 24 '06 #3
(i forgot to say it didn't work)

Apr 24 '06 #4
gangesmaster:
(i forgot to say it didn't work)


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
Apr 24 '06 #5
gangesmaster wrote:
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?

call thread.interrupt_main()

on *NIX: os.kill(os.getpid(),signal.XXXX)

or best design your threading correctly with resonable/flexible
communication channels, e.g. CallQueue:
http://aspn.activestate.com/ASPN/Coo.../Recipe/491281

(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

Apr 24 '06 #6
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.getpid()), or TerminateProcess(-1) but that's not
what i want
-tomer

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

Apr 24 '06 #8
gangesmaster wrote:
that's not a question of design. i just want a child-thread to kill the
process. in a platform agnostic way.


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

so thread.interrupt_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
KeyboardInterrupt is thrown, like with ..

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

... its thrown exactly app-deterministic a callqueue.receive() 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".

Apr 24 '06 #9
robert wrote:
killing hard (SIGKILL etc ) is low level. it should be OS-specific, as
Python should not make itself inconsistent


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

-robert
Apr 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mr. Mountain | last post: by
4 posts views Thread by MSDousti | last post: by
3 posts views Thread by Keith Mills | last post: by
reply views Thread by Lloyd Zusman | last post: by
4 posts views Thread by Marcus Alves Grando | last post: by
7 posts views Thread by Brendon Costa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.