By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

stoppable child thread

P: n/a
I am trying to use Peter's StoppableThread(threading.Thread).

What I want to do is to start 5 child threads, then do something, then
when got ^C keyboard exception, stop the child thread.

For some reason (apparently strange for me :) ), the child threads can
NOT be stopped.

See the enclosed code for more info,

Thanks and have a nice Xmas.

Jimmy

============

#!/usr/bin/env python

import threading, thread
from time import sleep, ctime
class StoppableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._running = True
def stop(self):
self._running = False
def run(self):
while self._running:
# do stuff here that loops periodically to allow
# the flag to be checked
print 'start loop at:', ctime()
sleep(4)

def main():
threads = []
try:
for i in range(5):
t = StoppableThread()
t.start()
sleep(0.001)
threads.append(t)
except KeyboardInterrupt:
for t in threads:
t.stop()
t.join()
del threads[:]
print 'child thread exiting...'+'/n/n'

if __name__ == '__main__':
main()

Dec 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ja*******@yahoo.com ha escrito:
def main():
threads = []
try:
for i in range(5):
t = StoppableThread()
t.start()
sleep(0.001)
threads.append(t)
except KeyboardInterrupt:
for t in threads:
t.stop()
t.join()
del threads[:]
print 'child thread exiting...'+'/n/n'
Put a print statement just at the end of main() and see what happens.
You said:
What I want to do is to start 5 child threads, then do something, then
when got ^C keyboard exception, stop the child thread.
You've created the threads, you've handled ^C, what's missing...?

--
Gabriel Genellina

Dec 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.