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

Commit

Permalink
Cleanup should_sample logic that differentiates b/w dev and prod envi…
Browse files Browse the repository at this point in the history
…ronments. Also add more explanatory comments.
  • Loading branch information
kamens committed Jan 5, 2013
1 parent 185921b commit e2127f2
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions sampling_profiler.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,19 +117,22 @@ def take_sample(self):
# See http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/ # See http://bzimmer.ziclix.com/2008/12/17/python-thread-dumps/
for thread_id, stack in sys._current_frames().items(): for thread_id, stack in sys._current_frames().items():


# ...and choose to sample only the main request thread. # ...but only sample from 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 if _is_dev_server:
# than one of them. # In development, current_request_thread_id won't be set
should_sample = thread_id == self.current_request_thread_id # properly. threading.current_thread().ident always returns -1

# in dev. So instead, we just take a peek at the stack's
# Dev server's threading.current_thread() implementation won't give # current package to figure out if it is the request thread.
# us a thread id that we can use. Instead, just take a peek at the should_sample = (stack.f_globals["__package__"] ==
# stack's current package to figure out which is the request "gae_mini_profiler")
# thread. else:
if (_is_dev_server and # In production, current_request_thread_id will be set properly
stack.f_globals["__package__"] == "gae_mini_profiler"): # by threading.current_thread().ident.
should_sample = True # 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: if should_sample:
# Grab a sample of this thread's current stack # Grab a sample of this thread's current stack
Expand Down

0 comments on commit e2127f2

Please sign in to comment.