A while back I implemented a debugging and profiling middleware, and I’ve been using those with Pylons recently. I think their pretty useful, so I’ve wrapped them up into an installable egg that contains Paste interfaces in setup.py. This allows you to easily insert the middleware into any existing Paste project (e.g. a Pylons project). I’m basically going to use this middlewares project as a dumping ground for middleware I find useful. It currently contains a debug (via DBGP) middleware, a profiler and a csrf middleware. There is nothing that says you must use Paste with these, Paste just makes it easier. The csrf middleware is currently tied to using Beaker.
Here’s a screenshot of Komodo IDE debugging my Pylons app. Lots of other debuggers support DBGP, but for many reasons I like Komodo.
Below is a partial output of line profiling the csrf middleware call handler. The profiler can use line or call profiling. I find line profiling handy when I want to focus into a specific area. While debugging and call profiling require no code changes in your code, line profiling does require you to decorate the function(s) you want to line profile.
The profiler currently requires a patch if you want to do line profiling with Pylons. I’ve sent the patch to the maintainer of line_profiler.
Timer unit: 1e-06 s File: ...../csrf.py Function: __call__ at line 40 Total time: 0.004621 s Line # Hits Time Per Hit % Time Line Contents ============================================================== 40 @profile 41 def __call__(self, environ, start_response): 42 1 29 29.0 0.6 request = Request(environ) 43 1 3 3.0 0.1 session = environ['beaker.session'] 44 1 476 476.0 10.3 csrf_token = session.get('csrf') 45 1 4 4.0 0.1 if not csrf_token: 46 csrf_token = session['csrf'] = str(random.getrandbits(128)) 47 session.save() 48 49 1 7 7.0 0.2 if request.method == 'POST':