How can I profile a multithread program in Python?

前端 未结 7 616
囚心锁ツ
囚心锁ツ 2020-11-30 04:12

I\'m developing an inherently multithreaded module in Python, and I\'d like to find out where it\'s spending its time. cProfile only seems to profile the main thread. Is the

7条回答
  •  离开以前
    2020-11-30 05:13

    If you're okay with doing a bit of extra work, you can write your own profiling class that implements profile(self, frame, event, arg). That gets called whenever a function is called, and you can fairly easily set up a structure to gather statistics from that.

    You can then use threading.setprofile to register that function on every thread. When the function is called you can use threading.currentThread() to see which it's running on. More information (and ready-to-run recipe) here:

    http://code.activestate.com/recipes/465831/

    http://docs.python.org/library/threading.html#threading.setprofile

提交回复
热议问题