import threading import warnings import time class MyThread(threading.Thread): def run(self): with warnings.catch_warnings(True) as self.warninglist: for i in range(10): # warnings.warn("%s-w%d" % (self.name, i)) warnings.warn(self.name) time.sleep(0.1) print "warnings.showwarning=%s" % warnings.showwarning warnings.simplefilter("always") t1 = MyThread(name="Thread1") t2 = MyThread(name="Thread2") t1.start() time.sleep(0.5) t2.start() t1.join() t2.join() # both lists should contain 10 items, all of them equal print "%s catched %d warnings: %s" % (t1.name, len(t1.warninglist), [str(w.message) for w in t1.warninglist]) print "%s catched %d warnings: %s" % (t2.name, len(t2.warninglist), [str(w.message) for w in t2.warninglist]) # keep a reference to the first thread warning list - the thread is dead by now warninglist = t1.warninglist assert not t1.is_alive() del t1 del t2 print "last warning recorded=%s" % warninglist[-1].message # this warning should appear on console, but is still catched into t1.warninglist warnings.warn("This should not be catched!") print "after another warn call, last warning recorded=%s" % warninglist[-1].message # warnings.showwarning should be the original function by now, but isn't print "warnings.showwarning=%s" % warnings.showwarning """ Output: warnings.showwarning= error-warnings.py:10: UserWarning: Thread2 warnings.warn(self.name) error-warnings.py:10: UserWarning: Thread2 warnings.warn(self.name) error-warnings.py:10: UserWarning: Thread2 warnings.warn(self.name) error-warnings.py:10: UserWarning: Thread2 warnings.warn(self.name) Thread1 catched 5 warnings: ['Thread1', 'Thread1', 'Thread1', 'Thread1', 'Thread 1'] Thread2 catched 11 warnings: ['Thread2', 'Thread1', 'Thread2', 'Thread1', 'Threa d2', 'Thread1', 'Thread2', 'Thread1', 'Thread2', 'Thread1', 'Thread2'] last warning recorded=Thread1 after another warn call, last warning recorded=This should not be catched! warnings.showwarning= """