Skip to content

Instantly share code, notes, and snippets.

@jimjh

jimjh/gevent_tracer.py

Last active Jun 22, 2020
Embed
What would you like to do?
gevent_tracer.py
def _callback(event, args):
# switch and throw both switch the active greenlet from origin to target
if event not in set(['switch', 'throw']):
return
origin, target = args
# record last switch (time and CPU tick)
global __last_switch_time_ms, __last_switch_cpu_tick
then_time, then_tick = __last_switch_time_ms, __last_switch_cpu_tick
now_time, now_tick = switch_timer()
__last_switch_time_ms, __last_switch_cpu_tick = now_time, now_tick
# first switch, nothing to record
if None in (then_time, then_tick):
return
if origin is gevent.hub.get_hub():
return
elapsed_ms = now_time - then_time
elapsed_ticks = now_tick - then_tick
if elapsed_ms <= threshold_ms:
return
log_fn(timing=elapsed_ms, value=elapsed_ticks, reason=trace_info) # log extra information e.g. stack frames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment