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?