Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profile Python CPU Usage By Thread

Tags:

python

I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage.

I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the application. I understand that os.times() will get CPU usage overall - is there something I can run from within each thread to get each thread's usage? It would be very helpful to determine which thread is eating CPU.

like image 510
Dave Avatar asked Jan 12 '10 15:01

Dave


2 Answers

Or you can simply use yappi. (https://code.google.com/p/yappi/) It transparently uses GetThreadTimes() if CPU clock type is selected for profiling. It will show you CPU time of a running thread.

See here also for an example: https://code.google.com/p/yappi/wiki/YThreadStats_v082

like image 93
Sumer Cip Avatar answered Oct 01 '22 20:10

Sumer Cip


Other than the profiler, I don't think Python has much built-in for monitoring individual thread CPU usage. Also, this post provides an example of a simple implementation of a thread profiler. It appears mainly geared towards Unix-based systems, but it's a start.

like image 33
Cerin Avatar answered Oct 01 '22 22:10

Cerin