threads and sys.exit()

G

gangesmaster

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
 
D

Diez B. Roggisch

gangesmaster said:
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
 
R

Rene Pijlman

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.
 
R

robert

gangesmaster said:
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/Cookbook/Python/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
 
G

gangesmaster

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
 
G

gangesmaster

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

robert

gangesmaster said:
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".
 
R

robert

robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top