Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 10th, 2004, 10:51 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old August 11th, 2004, 07:10 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old August 11th, 2004, 01:32 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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() 

Reply With Quote
  #4  
Old August 11th, 2004, 03:09 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
Quote:
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".

Reply With Quote
  #5  
Old August 11th, 2004, 04:13 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
thats good to know. thanks.

Reply With Quote
  #6  
Old August 11th, 2004, 05:16 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,221 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 22 h 21 m 4 sec
Reputation Power: 263
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:
Quote:
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

Reply With Quote
  #7  
Old August 11th, 2004, 05:21 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
thanks alot :)

Reply With Quote
  #8  
Old August 11th, 2004, 06:03 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Nevermind all that, I figured it out. Thanks for your help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > sys.exit() not closing the program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway