import sys import time import threading import weakref class A: def __init__(self): self.cycle = self def my_thread(): d = weakref.WeakValueDictionary() while 1: a = A() d[42] = a assert 42 in d # 'a' is alive at least until here, so the assert above should # always pass---but it does not. threading.Thread(target=my_thread).start() threading.Thread(target=my_thread).start() if hasattr(sys, 'setswitchinterval'): sys.setswitchinterval(1e-6) # Python 3.x else: sys.setcheckinterval(10) # Python 2.7 time.sleep(10)