import multiprocessing import threading import sys import time def do_stderr(queue): class f: def write(self, data): queue.put(str(time.time()) + data) def flush(self): pass sys.stderr = f() def g(): print(time.time()) g() g() if __name__ == '__main__': q = multiprocessing.Queue() # when used with Thread, identical to using Queue/queue module #try: # import Queue #except ImportError: # import queue as Queue #q = Queue.Queue() # When the Process finally dies, a lot of information has been put # onto the Queue but it hasn't necessarily had enough time to be # synced to the parent process so it may be only a truncated subset # of the data that remains available on the parent. multiprocessing.Process(target=do_stderr,args=(q,)).start() #threading.Thread(target=do_stderr,args=(q,)).start() # Swapping from Process to Thread changes how information appears # on the Queue (one line per put() instead of all lines in a single # put()). # Comparing runs between Python 2 and 3 can be insightful as well.