I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): ... thread.out = [] ... while thread.running: ... time.sleep(.01) ... thread.out.append(".") >>> thread = threading.Thread(None, example) >>> thread.running = True; thread.start() >>> time.sleep(.1) >>> thread.running = False >>> print thread.out ['.', '.', '.', '.', '.', '.', '.', '.', '.'] """
if __name__ == "__main__": import doctest; doctest.testmod() $ python doctest-threads.py Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap self.run() File "/usr/lib/python2.4/threading.py", line 422, in run self.__target(*self.__args, **self.__kwargs) File "<doctest __main__[1]>", line 5, in example NameError: global name 'thread' is not defined I have found out a workaround, putting 'thread' in the main program (i.e. in the globals): $ cat doctest-threads2.py """ >>> thread.running = True >>> thread.start() >>> time.sleep(.1) >>> thread.running = False >>> print thread.out ['.', '.', '.', '.', '.', '.', '.', '.', '.'] """ import time, threading def example(): thread.out = [] while thread.running: time.sleep(.01) thread.out.append(".") thread = threading.Thread(None, example) if __name__ == "__main__": import doctest; doctest.testmod() However this is strange, since replacing in the first script >>> globals()["thread"] = threading.Thread(None, example) does NOT work, so it is not just putting stuff in the globals. Also, it seems that I cannot reproduce the same error in absense of threads. Any idea of what is happening? Thanks for sharing, Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list