def greenlet_profiler(event, args):
if event in ('switch', 'throw'): # both events are in the target context
now = clock()
try:
# we need to account the time for the user function
frame = sys._getframe(1)
except ValueError:
# the first greenlet.switch() and when the greenlet is being
# destroied there is nothing more in the stack, so this function is
# the first function called
frame = sys._getframe(0)
origin, target = args
origin_state = _state[origin]
target_state = ensure_thread_state(target, frame)
origin_state.switch_enter(now) # origin is entering the "sleep" state
target_state.switch_exit(now) # target might be leaving the "sleep"
评论列表
文章目录