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 there any way of profiling all threads involved in the calculation?
我正在用Python开发一个固有的多线程模块,我想知道它在哪里花费时间。cProfile似乎只关注主线程。是否有办法对计算中涉及的所有线程进行概要分析?
5 个解决方案
#2
14
Instead of running one cProfile
, you could run separate cProfile
instance in each thread, then combine the stats. Stats.add()
does this automatically.
不需要运行一个cProfile,您可以在每个线程中运行单独的cProfile实例,然后合并stats。这个自动Stats.add()。
#3
4
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.
如果您可以做一些额外的工作,您可以编写自己的剖析类来实现概要文件(self、frame、event、arg)。每当调用一个函数时都会调用它,您可以很容易地设置一个结构来收集统计信息。
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:
然后可以使用线程。在每个线程上注册该函数的setprofile。调用该函数时,可以使用thread . currentthread()查看它在哪个函数上运行。更多信息(和准备运行的配方):
http://code.activestate.com/recipes/465831/
http://code.activestate.com/recipes/465831/
http://docs.python.org/library/threading.html#threading.setprofile
http://docs.python.org/library/threading.html threading.setprofile
#4
1
Given that your different threads' main functions differ, you can use the very helpful profile_func()
decorator from here.
由于不同线程的主要功能不同,您可以从这里使用非常有用的profile_func() decorator。
#5
-1
I don't know any profiling-application that supports such thing for python - but You could write a Trace-class that writes log-files where you put in the information of when an operation is started and when it ended and how much time it consumed.
我不知道任何支持python的分析器应用程序——但是您可以编写一个跟踪类来编写日志文件,您可以在其中输入操作何时启动、何时结束以及花费多少时间的信息。
It's a simple and quick solution for your problem.
这是一个简单而快速的解决方案。
#1
#2
14
Instead of running one cProfile
, you could run separate cProfile
instance in each thread, then combine the stats. Stats.add()
does this automatically.
不需要运行一个cProfile,您可以在每个线程中运行单独的cProfile实例,然后合并stats。这个自动Stats.add()。
#3
4
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.
如果您可以做一些额外的工作,您可以编写自己的剖析类来实现概要文件(self、frame、event、arg)。每当调用一个函数时都会调用它,您可以很容易地设置一个结构来收集统计信息。
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:
然后可以使用线程。在每个线程上注册该函数的setprofile。调用该函数时,可以使用thread . currentthread()查看它在哪个函数上运行。更多信息(和准备运行的配方):
http://code.activestate.com/recipes/465831/
http://code.activestate.com/recipes/465831/
http://docs.python.org/library/threading.html#threading.setprofile
http://docs.python.org/library/threading.html threading.setprofile
#4
1
Given that your different threads' main functions differ, you can use the very helpful profile_func()
decorator from here.
由于不同线程的主要功能不同,您可以从这里使用非常有用的profile_func() decorator。
#5
-1
I don't know any profiling-application that supports such thing for python - but You could write a Trace-class that writes log-files where you put in the information of when an operation is started and when it ended and how much time it consumed.
我不知道任何支持python的分析器应用程序——但是您可以编写一个跟踪类来编写日志文件,您可以在其中输入操作何时启动、何时结束以及花费多少时间的信息。
It's a simple and quick solution for your problem.
这是一个简单而快速的解决方案。