question about ctrl-d and atexit with threads

Darren Dale dsdale24 at
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 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,

import atexit
import threading
import time

class MyThread(threading.Thread):

    def __init__(self):

        self.lock = threading.Lock()
        self.stopEvent = threading.Event()

    def run(self):
        while not self.stopEvent.isSet():
            # print 'running'

    def stop(self):

my_thread = MyThread()

def stop_execution():



More information about the Python-list mailing list