import threading import traceback def say_what(): print "what" def say_hello(): print '-'*200 print "hello" say_what() def profiler(frame, event, args): fname, lineno, func, _ = traceback.extract_stack(f=frame)[-1] if (fname == '/usr/lib/python2.7/threading.py' and lineno == 752 and func == 'run' and event == 'call'): print '-'*20 print threading.current_thread().name traceback.print_stack(f=frame) print args print '+'*20 if (fname == '/usr/lib/python2.7/threading.py' and lineno == 874 and func == '__stop' and event == 'return'): print '*'*20 print threading.current_thread().name traceback.print_stack(f=frame) print args print '/'*20 def main(): import sys threading.setprofile(profiler) t = threading.Thread(target=say_hello) t.start() t.join() # import time # time.sleep(1) print "done"