Permalink
Browse files

Cleanup should_sample logic that differentiates b/w dev and prod envi…

…ronments. Also add more explanatory comments.
  • Loading branch information...
1 parent 185921b commit e2127f2820a31f46ba8b950693f0255f1c215ac9 @kamens kamens committed Jan 5, 2013
Showing with 16 additions and 13 deletions.
  1. +16 −13 sampling_profiler.py
View
@@ -117,19 +117,22 @@ def take_sample(self):
# See http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/
for thread_id, stack in sys._current_frames().items():
- # ...and choose to sample only the main request thread.
- # TODO(kamens): this profiler will need work if we ever actually
- # use multiple threads in a single request and want to profile more
- # than one of them.
- should_sample = thread_id == self.current_request_thread_id
-
- # Dev server's threading.current_thread() implementation won't give
- # us a thread id that we can use. Instead, just take a peek at the
- # stack's current package to figure out which is the request
- # thread.
- if (_is_dev_server and
- stack.f_globals["__package__"] == "gae_mini_profiler"):
- should_sample = True
+ # ...but only sample from the main request thread.
+
+ if _is_dev_server:
+ # In development, current_request_thread_id won't be set
+ # properly. threading.current_thread().ident always returns -1
+ # in dev. So instead, we just take a peek at the stack's
+ # current package to figure out if it is the request thread.
+ should_sample = (stack.f_globals["__package__"] ==
+ "gae_mini_profiler")
+ else:
+ # In production, current_request_thread_id will be set properly
+ # by threading.current_thread().ident.
+ # TODO(kamens): this profiler will need work if we ever
+ # actually use multiple threads in a single request and want to
+ # profile more than one of them.
+ should_sample = thread_id == self.current_request_thread_id
if should_sample:
# Grab a sample of this thread's current stack

0 comments on commit e2127f2

Please sign in to comment.