question about ctrl-d and atexit with threads

Darren Dale dsdale24 at gmail.com
Thu Mar 5 16:13:59 CET 2009


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:

>>> from test import my_thread
>>> import sys
>>> sys.exit()

If instead I do the following:

>>> from test import my_thread
>>> <invoke ctrl-D>

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()



More information about the Python-list mailing list