August 10th, 2004, 11:51 PM
sys.exit() not closing the program
i've written a nice, multithreaded server that will be used in an upcoming service. that said, we were having problems killing the processes; they were causing more trouble than they should. the solution was to make it so when the server receives the command "die", it shuts itself down, thus avoiding the trouble of killing it ourselves. However, quite simply, sys.exit() will not work.
My impression is that all its doing is exiting the thread that interprets commands, because after you send "die", the server doesn't close, it just stops accepting commands. Note" I know for a fact that sys.exit() is called.
Also, I don't want to use os._exit() because of how sloppy it is (as explained in the documentation. if this worry is unfounded, please let me know.) Does anyone have a suggestion for how to solve this problem, or a thought as to why this is happening?
August 11th, 2004, 08:10 AM
Without seeing your code its kinda hard to imagin what's going on. Maybe you could attach the program in question, or at least, post the problem area.
If what your saying is right, and it sounds possible then you need a way to get the thread to shut down the whole program.. triggering SystemExit from outside the thread might work?
Personally, I find raising SystemExit cleaner than using sys.exit(); but both work in the same way.
August 11th, 2004, 02:32 PM
Yeah, raising SystemExit does the same thing as sys.exit(). Anyway, here's the offending code:
class IterQueue( threading.Thread ):
def run( self ):
get = recvd.get()
self.interpret( get )
def interpret( self , cmdstr ):
cmdstr = cmdstr.strip()
print "Interpreting: " + cmdstr # strictly a debugging feature; will be removed
if cmdstr == "die":
August 11th, 2004, 04:09 PM
AFAIK raising SystemExit permits you to exit without importing the sys modules, hence "cleaner".
Originally Posted by dbickett
August 11th, 2004, 05:13 PM
thats good to know. thanks.
August 11th, 2004, 06:16 PM
Yep, calling sys.exit() or raising SystemExit in any thread other than the main one will kill the thread, but not the program. This is the expected behaviour, but it is not very well documented.
From the docs for the thread module:
The correct way to end the program is to send a signal to the main thread that it is time to finish, and call sys.exit from there.
You could do this with a global threading.Event object.
Dave - The Developers' Coach
August 11th, 2004, 06:21 PM
August 11th, 2004, 07:03 PM
Nevermind all that, I figured it out. Thanks for your help