From 82cbb9a1d100c4865059fa35eccb3506fc2c6d39 Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Sun, 15 Jan 2012 16:15:32 -0500 Subject: [PATCH] Matched stop and start calls in perf middleware. --- perfstats/middleware.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/perfstats/middleware.py b/perfstats/middleware.py index 87f2a6e6c6..ac10b59a47 100644 --- a/perfstats/middleware.py +++ b/perfstats/middleware.py @@ -5,17 +5,21 @@ from django.conf import settings tmpfile = None prof = None +isRunning = False def restart_profile(): - global prof, tmpfile + global prof, tmpfile, isRunning try: oldname = tmpfile.name except: oldname = "" if prof != None: + if isRunning: + prof.stop() prof.close() tmpfile = tempfile.NamedTemporaryFile(prefix='prof',delete=False) - prof = hotshot.Profile(tmpfile.name) + prof = hotshot.Profile(tmpfile.name, lineevents=1) + isRunning = False print "Filename", tmpfile.name return (tmpfile.name, oldname) @@ -23,13 +27,19 @@ restart_profile() class ProfileMiddleware: def process_request (self, request): - prof.start() + global isRunning + if not isRunning: + prof.start() + isRunning = True + else: + print "Profiler was already running. Results may be unpredictable" + print "Process request" def process_response (self, request, response): - try: + global isRunning + if isRunning: prof.stop() - except: - print "Profiler not active. If you didn't just restart, this is an error" + isRunning = False print "Process response" return response