Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Added locking to the WSGI profiler middleware since it uses a lot of …
Browse files Browse the repository at this point in the history
…local variables
  • Loading branch information
cdman committed Jul 27, 2012
1 parent 3be8882 commit 5d065fe
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions profiler.py
Expand Up @@ -325,6 +325,7 @@ class ProfilerWSGIMiddleware(object):

def __init__(self, app):
template.register_template_library('gae_mini_profiler.templatetags')
self._lock = threading.RLock()
self.app = app
self.app_clean = app
self.prof = None
Expand All @@ -337,18 +338,24 @@ def __init__(self, app):
self.end = None

def __call__(self, environ, start_response):

# Start w/ a non-profiled app at the beginning of each request
self.app = self.app_clean
self.prof = None
self.recorder = None
self.temporary_redirect = False
self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1"
requeststore.clear_id()

# Never profile calls to the profiler itself to avoid endless recursion.
if config.should_profile(environ) and not environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"):
if not config.should_profile(environ) or environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"):
result = self.app(environ, start_response)
for value in result:
yield value
return

try:
self._lock.acquire()

# Start w/ a non-profiled app at the beginning of each request
self.app = self.app_clean
self.prof = None
self.recorder = None
self.temporary_redirect = False
self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1"
requeststore.clear_id()

# Set a random ID for this request so we can look up stats later
request_id = requeststore.generate_id()

Expand Down Expand Up @@ -439,10 +446,8 @@ def wrapped_appstats_app(environ, start_response):
self.prof = None
requeststore.clear_id()

else:
result = self.app(environ, start_response)
for value in result:
yield value
finally:
self._lock.release()

def add_handler(self):
if self.handler is None:
Expand Down

0 comments on commit 5d065fe

Please sign in to comment.