Profiling threads?

Gregory P. Smith greg at electricrain.com
Tue Jul 17 21:39:12 EDT 2001


On Tue, Jul 17, 2001 at 06:33:59PM -0400, Johns, Richard [NCRTP:JZ54:EXCH] wrote:
> Any suggestions on using profile with threaded programs?  I haven't managed
> to get the profiler to do anything in a thread yet.  Is it a matter of 
> getting the thread to exit on its own so the profiler can dump its report?

While the profiler doesn't appear to support profiling the overall
execution of a program regardless of the threads you can profile the
performance on individual threads (even at the same time).  We use the
following function to call the main loop of our worker threads (such
as the asyncore select loop and our seperate event queue thread):

def _dont_enable_if_you_want_speed_profit(func):
    result = None
    p = profile.Profile()
    try:
        result = p.runcall(func)
    finally:
        if hasattr(func, 'func_name'):
            tmpfname = tempfile.mktemp() + func.func_name
        elif hasattr(func, 'im_func') and hasattr(func.im_func, '__name__'):
            tmpfname = tempfile.mktemp() + func.im_func.__name__
        else:
            tmpfname = tempfile.mktemp() + "unknownfuncname"

        debug.write("thread finished %s\n", args=(tmpfname,), v=0, vs="debug")

        p.dump_stats(tmpfname)
        p = None
        del p

    return result

Yes, as this implies, you -do- need the thread to exit in order for
the profiler to dump its report.  I suggest creating a threading.Event
object that your thread checks using isSet() periodically to see if it
should exit.  (or when the thread sleeps it should wait on the event
rather than using time.sleep).

-- 
Gregory P. Smith   gnupg/pgp: http://electricrain.com/greg/keys/
                   C379 1F92 3703 52C9 87C4  BE58 6CDA DB87 105D 9163




More information about the Python-list mailing list