import sys import thread import time import cgitb def foo(): print cgitb.html(sys.exc_info()), thread.start_new_thread(foo, ()) # Don't let main thread exit yet time.sleep(5)