Sys.exit() does not fully exit

H

Harlin Seritt

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
 
C

Christian Heimes

Harlin said:
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
 
G

Gary Herron

Christian said:
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
 
C

Christian Heimes

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

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top