views:

338

answers:

2

How can I profile a python script running on mod_wsgi on apache

I would like to use cProfile but it seems it requires me to invoke a function manually. Is there a way to enable cProfile globally and have it keep on logging results.

+5  A: 

You need to wrap you wsgi application function inside another function that just calls your function using cProfile and use that as the application. Or you can reuse existing WSGI middleware to do that for you, for example repoze.profile does pretty much what you seem to want.

Ants Aasma
repoze.profile worked perfectly for my task!
The Unknown
A: 

Here is the WSGI profile middleware for WHIFF (currently only available from the mercurial repository): profile.py. That should get you started. If you want to modify it to run outside of the WHIFF context change the line

 gateway.putResource(env, resourcePath, report)

to something like

 file("/tmp/profile.txt", "w").write(report)
Aaron Watters