#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10

    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?

    Thanks,
    Beeyah
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    Yeah, raising SystemExit does the same thing as sys.exit(). Anyway, here's the offending code:

    PHP Code:
    class IterQueuethreading.Thread ):
        
    def runself ):
            while 
    1:
                
    get recvd.get()
                
    self.interpretget[0] )
        
    def interpretself cmdstr ):
            
    cmdstr cmdstr.strip()
            print 
    "Interpreting: " cmdstr # strictly a debugging feature; will be removed
            
    if cmdstr == "die":
                
    sys.exit() 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Originally Posted by dbickett
    Yeah, raising SystemExit does the same thing as sys.exit().
    AFAIK raising SystemExit permits you to exit without importing the sys modules, hence "cleaner".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    thats good to know. thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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:
    Calling sys.exit() or raising the SystemExit exception is equivalent to calling exit().
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    thanks alot :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    Nevermind all that, I figured it out. Thanks for your help

IMN logo majestic logo threadwatch logo seochat tools logo