Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def start_profiler():
global _state
assert _state
_state = GlobalState()
frame = sys._getframe(0)
current_greenlet = greenlet.getcurrent() # pylint: disable=no-member
thread_state = ensure_thread_state(current_greenlet, frame)
_state.last = thread_state
# this needs to be instantiate before the handler is installed
greenlet.settrace(greenlet_profiler) # pylint: disable=no-member
sys.setprofile(thread_profiler)
threading.setprofile(thread_profiler)
def start_profiler():
global _state
_state = GlobalState()
frame = sys._getframe(0)
current_greenlet = greenlet.getcurrent() # pylint: disable=no-member
thread_state = ensure_thread_state(current_greenlet, frame)
_state.last = thread_state
# this needs to be instantiate before the handler is installed
greenlet.settrace(greenlet_profiler) # pylint: disable=no-member
sys.setprofile(thread_profiler)
threading.setprofile(thread_profiler)
HUB = gevent.get_hub()
global threading
import threading
for thread in threading.enumerate():
_set_thread_uuid(thread.ident)
_set_main_uuid() # the patched threading has a new ident for the main thread
# this will declutter the thread dumps from gevent/greenlet frames
from .threadtree import _BOOTSTRAPPERS
import gevent, gevent.threading, gevent.greenlet
_BOOTSTRAPPERS.update([gevent, gevent.threading, gevent.greenlet])
if hogging_detection:
import greenlet
greenlet.settrace(lambda *args: _greenlet_trace_func(*args))
defer_to_thread(detect_hogging, 'detect-hogging')
def bench_trivial_tracer(loops):
def trivial(_event, _args):
return
greenlet.settrace(trivial)
try:
return _run(loops)
finally:
greenlet.settrace(None)
def stop_profiler():
# we keep the _state around for the user until the next session
# Unregister the profiler in this order, otherwise we will have extra
# measurements in the end
sys.setprofile(None)
threading.setprofile(None)
greenlet.settrace(None) # pylint: disable=no-member
def serve_forever(self):
ctx = context.get_context()
ctx.running = True
try:
# set up greenlet switch monitoring
import greenlet
greenlet.settrace(ctx._trace)
except AttributeError:
pass # oh well
if not self.prefork:
self.start()
log_msg = 'Group initialized and serving forever...'
ctx.log.critical('GROUP.INIT').success(log_msg)
if ctx.dev and ctx.dev_service_repl_enabled and os.isatty(0):
if not hasattr(os, "getpgrp"): # Windows
fg = True
else:
try:
fg = os.getpgrp() == os.tcgetpgrp(sys.stdout.fileno())
except OSError:
fg = False
if fg:
# only start REPL on unix machines if running in foreground
def kill(self):
# Must be called in the monitored thread.
if not self._killed:
self._killed = True
settrace(self.previous_trace_function)
self.previous_trace_function = None
print("from %s switch to %s" % (src, target))
elif event == "throw":
print("from %s throw exception to %s" % (src, target))
if src.gr_frame:
tracebacks = inspect.getouterframes(src.gr_frame)
buff = []
for traceback in tracebacks:
srcfile, lineno, func_name, codesample = traceback[1:-1]
trace_line = '''File "%s", line %d, in %s\n%s '''
buff.append(trace_line %
(srcfile, lineno, func_name, "".join(codesample)))
print("".join(buff))
greenlet.settrace(trace_green)
def init_process(self):
# Set up a greenlet tracing hook to monitor for event-loop blockage,
# but only if monitoring is both possible and required.
if hasattr(greenlet, "settrace") and \
self.app.cfg.gevent_check_interval > 0:
# Grab a reference to the gevent hub.
# It is needed in a background thread, but is only visible from
# the main thread, so we need to store an explicit reference to it.
self._active_hub = gevent.hub.get_hub()
# Set up a trace function to record each greenlet switch.
self._active_greenlet = None
self._greenlet_switch_counter = 0
greenlet.settrace(self._greenlet_switch_tracer)
self._main_thread_id = _real_get_ident()
# Create a real thread to monitor out execution.
# Since this will be a long-running daemon thread, it's OK to
# fire-and-forget using the low-level start_new_thread function.
_real_start_new_thread(self._process_monitoring_thread, ())
return super(GeventThriftPyWorker, self).init_process()
{
"event": "Switching",
"origin": str(origin),
"target": str(target),
"target_callstack": callstack,
"time": time.time(),
}
)
)
if previous_callback is not None:
return previous_callback(event, args)
return None
greenlet.settrace(log_every_switch)
return previous_callback