本文整理汇总了Python中greenlet.settrace方法的典型用法代码示例。如果您正苦于以下问题:Python greenlet.settrace方法的具体用法?Python greenlet.settrace怎么用?Python greenlet.settrace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类greenlet
的用法示例。
在下文中一共展示了greenlet.settrace方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_process
# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import settrace [as 别名]
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()
示例2: enable_debug
# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import settrace [as 别名]
def enable_debug():
if IS_PYPY:
sys.stderr.write("settrace api unsupported on pypy")
sys.stderr.flush()
return
import inspect
def trace_green(event, args):
src, target = args
if event == "switch":
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)
示例3: enable_greenlet_tracing
# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import settrace [as 别名]
def enable_greenlet_tracing():
# Tracing seems to cause a 2-5% performance loss.
import greenlet
greenlet.GREENLET_USE_TRACING = True
def trace(*args):
time_since_last_switch = time.time() - trace.last_switch
# Record the time of the current switch
trace.last_switch = time.time()
if args[0] == "switch":
# We are switching from the greenlet args[1][0] to the greenlet
# args[1][1]
args[1][0].__dict__.setdefault("_trace_time", 0)
args[1][0].__dict__["_trace_time"] += time_since_last_switch
args[1][0].__dict__.setdefault("_trace_switches", 0)
args[1][0].__dict__["_trace_switches"] += 1
elif args[0] == "throw":
pass
trace.last_switch = time.time()
greenlet.settrace(trace) # pylint: disable=no-member
示例4: apply_patch
# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import settrace [as 别名]
def apply_patch(hogging_detection=False, real_threads=1):
_logger.info('applying gevent patch (%s real threads)', real_threads)
# real_threads is 1 by default so it will be possible to run watch_threads concurrently
if hogging_detection:
real_threads += 1
if real_threads:
_RealThreadsPool(real_threads)
_patch_module_locks()
import gevent
import gevent.monkey
for m in ["easypy.threadtree", "easypy.concurrency"]:
assert m not in sys.modules, "Must apply the gevent patch before importing %s" % m
gevent.monkey.patch_all(Event=True, sys=True)
_unpatch_logging_handlers_lock()
global HUB
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')
示例5: start
# 需要导入模块: import greenlet [as 别名]
# 或者: from greenlet import settrace [as 别名]
def start(self):
self.start_time = time.time()
greenlet.settrace(self._trace)
# Spawn a separate OS thread to periodically check if the active
# greenlet on the main thread is blocking.
gevent._threading.start_new_thread(self._monitoring_thread, ())