#!/usr/bin/env python import sys import threading def test_one_class(c): obj = c() e1 = threading.Event() e2 = threading.Event() def f1(): obj.x = 'foo' e1.set() e2.wait() def f2(): try: foo = obj.x except AttributeError: # This is expected -- we haven't set obj.x in this thread yet! sys.stderr.write('class %r: OK\n' % (c,)) else: sys.stderr.write('Incorrectly got value %r from class %r\n' % (foo, c)) t1 = threading.Thread(target=f1) t1.start() e1.wait() t2 = threading.Thread(target=f2) t2.start() t2.join() # The test is done; just let t1 know it can exit, and wait for it. e2.set() t1.join() class Subclass (threading.local): pass test_one_class(threading.local) test_one_class(Subclass)