D
Darren Dale
I have a function that stops execution of a thread, and this function
is registered with atexit.register. A simple example module is
included at the end of this post, say its called test.py. If I do the
following in the interactive interpreter, the thread stops executing
as I hoped:
If instead I do the following:
the interpreter hangs up and my_thread continues to execute
indefinitely (confirmed by uncommenting the print statement in run).
I've seen this behavior on python-2.5 and 2.6 on 64 bit linux systems
(gentoo and kubuntu). Can anyone else confirm that invoking ctrl-D
hangs up the interactive interpreter with this code? And if so, could
anyone explain how ctrl-d is different than sys.exit?
Thank you,
Darren
import atexit
import threading
import time
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.lock = threading.Lock()
self.stopEvent = threading.Event()
def run(self):
while not self.stopEvent.isSet():
# print 'running'
time.sleep(0.1)
def stop(self):
self.stopEvent.set()
self.join()
my_thread = MyThread()
def stop_execution():
my_thread.stop()
atexit.register(stop_execution)
my_thread.start()
is registered with atexit.register. A simple example module is
included at the end of this post, say its called test.py. If I do the
following in the interactive interpreter, the thread stops executing
as I hoped:
If instead I do the following:
the interpreter hangs up and my_thread continues to execute
indefinitely (confirmed by uncommenting the print statement in run).
I've seen this behavior on python-2.5 and 2.6 on 64 bit linux systems
(gentoo and kubuntu). Can anyone else confirm that invoking ctrl-D
hangs up the interactive interpreter with this code? And if so, could
anyone explain how ctrl-d is different than sys.exit?
Thank you,
Darren
import atexit
import threading
import time
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.lock = threading.Lock()
self.stopEvent = threading.Event()
def run(self):
while not self.stopEvent.isSet():
# print 'running'
time.sleep(0.1)
def stop(self):
self.stopEvent.set()
self.join()
my_thread = MyThread()
def stop_execution():
my_thread.stop()
atexit.register(stop_execution)
my_thread.start()