[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Sys.exit() does not fully exit

Harlin Seritt

2/17/2008 2:15:00 PM

I have this script:

import os, thread, threading, time, sys

class Script1(threading.Thread):
def run(self):
os.system('runScript1.py')

class Script2(threading.Thread):
def run(self):
os.system('runScript2.py')

if __name__ == '__main__':

s = Script1()
s.start()
time.sleep(5)

a = Script2()
a.start()
time.sleep(5)

while True:
answer = raw_input('Type "x" to shutdown: ')
if answer == 'x':
print 'Shutting down....'
time.sleep(1)
print 'Done'
sys.exit(0)

When x is typed, everything does shut down but the main script never
fully ends. Is there any way to get it to quit?

Thanks,

Harlin Seritt
3 Answers

Christian Heimes

2/17/2008 2:33:00 PM

0

Harlin Seritt wrote:
> When x is typed, everything does shut down but the main script never
> fully ends. Is there any way to get it to quit?

You don't need threads. Please use the subprocess module instead of
threads + os.system.

Christian

Gary Herron

2/17/2008 4:08:00 PM

0

Christian Heimes wrote:
> Harlin Seritt wrote:
>
>> When x is typed, everything does shut down but the main script never
>> fully ends. Is there any way to get it to quit?
>>
>
> You don't need threads. Please use the subprocess module instead of
> threads + os.system.
>
> Christian
>
That's a good answer. However, it you *do* want threads, and you don't
want the main thread to wait for the threads to quit, you can make the
threads "daemon threads". See setDaemon method on Thread objects in the
threading module.

Gary Herron

Christian Heimes

2/17/2008 5:05:00 PM

0

Gary Herron wrote:
> That's a good answer. However, it you *do* want threads, and you don't
> want the main thread to wait for the threads to quit, you can make the
> threads "daemon threads". See setDaemon method on Thread objects in the
> threading module.

In general you are right. But I don't think daemon threads are going to
solve the problem here. The threads may stop but Python may not have
returned from the system() syscall yet. In order to stop the syscall you
have to send a KILL or TERM signal to the child process. Threads and
signals don't mix well and you can get in all sorts of trouble.

Christian