Any way to use cProfile in a multi-threaded program?

Discussion in 'Python' started by mk, Dec 30, 2009.

  1. mk

    mk Guest

    I'm stumped; I read somewhere that one would have to modify Thread.run()
    method but I have never modified Python methods nor would I really
    want to do it.

    Is there any way to start cProfile on each thread and then combine the
    stats?

    Regards,
    mk
     
    mk, Dec 30, 2009
    #1
    1. Advertising

  2. On Dec 30, 3:00 pm, mk <> wrote:
    > I'm stumped; I read somewhere that one would have to modify Thread.run()
    >   method but I have never modified Python methods nor would I really
    > want to do it.
    >
    > Is there any way to start cProfile on each thread and then combine the
    > stats?
    >
    > Regards,
    > mk



    Well, you can start cProfile from any arbitrary point within your
    program, using the code:

    import cProfile
    import .mymodule
    command = 'mymodule.myfunction()'
    cProfile.runctx(command, globals(), locals(),
    filename='profile.out')

    Conventionally this is run at the start of your program (and myfunction
    () is something that you call to startup the remainder of your
    program), but one solution for you might be to run this after the
    threads have been created. You'll have to run this in each thread, and
    myfunction() will be the function required for the thread to do its
    work. Maybe each thread should output to a different filename, using
    something like filename='profile%s' % (threading.current_thread
    ().ident,)

    As for combining the outputs from each thread, I don't know what the
    cProfile output format is, so I can't help you there.

    There might be better ways, but I don't know them.
     
    Jonathan Hartley, Dec 30, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian douglas
    Replies:
    2
    Views:
    1,020
    Randy Howard
    Jul 30, 2004
  2. Nasif
    Replies:
    6
    Views:
    336
    Sani Rus
    Oct 9, 2008
  3. Nasif

    Testing Multi Threaded program on linux

    Nasif, Sep 18, 2008, in forum: C Programming
    Replies:
    7
    Views:
    377
    Antoninus Twink
    Oct 9, 2008
  4. C++Liliput
    Replies:
    3
    Views:
    443
    James Kanze
    Sep 24, 2008
  5. Angus
    Replies:
    5
    Views:
    323
    Brian Wood
    Nov 8, 2009
Loading...

Share This Page