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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.