tags:

views:

35

answers:

1

Hi all, I have an error from the following code. I am sure it is obviuous to someone with more Python experience. This is a snippet from http://djangosnippets.org/snippets/727/

import sys
import cProfile
from cStringIO import StringIO
from django.conf import settings

class ProfilerMiddleware(object):
    def process_view(self, request, callback, callback_args, callback_kwargs):
        if settings.DEBUG and 'prof' in request.GET:
            self.profiler = cProfile.Profile()
            args = (request,) + callback_args
            return self.profiler.runcall(callback, *args, **callback_kwargs)

    def process_response(self, request, response):
        if settings.DEBUG and 'prof' in request.GET:
            self.profiler.create_stats()
            out = StringIO()
            old_stdout, sys.stdout = sys.stdout, out
            self.profiler.print_stats(1)
            sys.stdout = old_stdout
            response.content = '<pre>%s</pre>' % out.getvalue()
        return response

I have implemented it as middleware and get the following error on pages.

Traceback (most recent call last):

  File "c:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "c:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)

  File "c:\Python26\lib\site-packages\django\core\handlers\wsgi.py", line 245, in __call__
    response = middleware_method(request, response)

  File "C:\Users\Richard\workspace\race\src\race\..\race\middleware\ProfilerMiddleware.py", line 15, in process_response
    self.profiler.create_stats()

AttributeError: 'ProfilerMiddleware' object has no attribute 'profiler'

Any ideas?

A: 

Thanks for your input Seth and Nick,

I fixed it. I don't understand what caused it but it is to do with the order my middleware is called in.

I debugged it based on Nick's comment that process_view hadn't been called to create the object. (I'm still not used to Python error messages to pick it up!) I got rid of process_response function and put a print statement in process_view and sure enough, the print statement was not output on the dev console proving process_view wasn't getting called. I suspected middleware settings order and got lucky.

I had settings as follows with these being the last 2 lines of my middleware setting:

Not working: 'firepython.middleware.FirePythonDjango', 'race.middleware.ProfilerMiddleware.ProfilerMiddleware',

Working: 'race.middleware.ProfilerMiddleware.ProfilerMiddleware', 'firepython.middleware.FirePythonDjango',

No idea why this is the case. I have some funny stuff going on with FirePython sometimes as well so maybe it's related. I have learned a bit so maybe stand a better chance of fixing FirePython as a result.

Thanks guys

Rich

Rich
I think the underlying problem is that `process_view` is a strange place to do the initial setup. I think `process_request` would be better, as that is more likely to be called.
Daniel Roseman
OK thanks Daniel, I will try that tomorrow.
Rich
Hi Daniel, I tried changing it to process_request but it didn't work at all. I think it's perhaps that the work the middleware needs to do such as calculating how many functions were called and how long they took to process happens after the request is made. Anyway, I will stick to the change in middleware order. I suspect the problem is with FirePython as I said but at least I am out of trouble for now. Thanks guys!
Rich