本文整理匯總了Python中gevent.hub方法的典型用法代碼示例。如果您正苦於以下問題:Python gevent.hub方法的具體用法?Python gevent.hub怎麽用?Python gevent.hub使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent
的用法示例。
在下文中一共展示了gevent.hub方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: exit
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def exit(self, signal=None):
""" Handle an exit request """
self.logger.info("{} {}".format(signal, "*" * 80))
# Kill the top level greenlet
gevent.kill(gevent.hub.get_hub().parent)
示例2: event_loop
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def event_loop(self):
try:
for fn, args, kwargs in self.queue:
try:
fn(*args, **kwargs)
except (v8.JSError, JSRuntimeException) as e:
self.log_output("Error running asynchronous JavaScript:")
self.log_output(e.stackTrace)
except gevent.hub.LoopExit:
logger.warning("Runtime ran out of events; terminating.")
示例3: __init__
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def __init__(self,
blocking_sample_period=BLOCKING_SAMPLE_PERIOD,
sampling_interval=GREENLET_SAMPLING_INTERVAL,
logging_interval=LOGGING_INTERVAL):
self.blocking_sample_period = blocking_sample_period
self.sampling_interval = sampling_interval
self.logging_interval = logging_interval
self.time_spent_by_context = collections.defaultdict(float)
self.total_switches = 0
self._last_switch_time = None
self._switch_flag = False
self._active_greenlet = None
self._main_thread_id = gevent._threading.get_ident()
self._hub = gevent.hub.get_hub()
self.last_logged_stats = time.time()
self.last_checked_blocking = time.time()
self.total_cpu_time = 0
self.process = psutil.Process()
self.pending_avgs = {1: 0, 5: 0, 15: 0}
self.cpu_avgs = {1: 0, 5: 0, 15: 0}
self.hostname = socket.gethostname().replace(".", "-")
self.process_name = str(config.get("PROCESS_NAME", "unknown"))
# We need a new client instance here because this runs in its own
# thread.
self.statsd_client = get_statsd_client()
self.start_time = time.time()
示例4: stats
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def stats(self):
total_time = time.time() - self.start_time
idle_fraction = self.time_spent_by_context.get('hub', 0) / total_time
return {
'times': self.time_spent_by_context,
'idle_fraction': idle_fraction,
'total_time': total_time,
'pending_avgs': self.pending_avgs,
'cpu_avgs': self.cpu_avgs,
'total_switches': self.total_switches
}
示例5: _trace
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def _trace(self, event, xxx_todo_changeme):
(origin, target) = xxx_todo_changeme
self.total_switches += 1
current_time = time.time()
if self._last_switch_time is not None:
time_spent = current_time - self._last_switch_time
if origin is not self._hub:
context = getattr(origin, 'context', None)
else:
context = 'hub'
self.time_spent_by_context[context] += time_spent
self._active_greenlet = target
self._last_switch_time = current_time
self._switch_flag = True
示例6: _make_gevent
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import hub [as 別名]
def _make_gevent():
import gevent
import gevent.hub
import gevent.queue
import greenlet
# We're importing socket to handle an known error in libev on Windows
# See rgalanakis/goless#28 and surfly/gevent#459
import socket
deadlock_errtypes = (gevent.hub.LoopExit,)
if _os.name == 'nt':
deadlock_errtypes += (SystemError,)
class Channel(gevent.queue.Channel):
def send(self, value):
with as_deadlock(deadlock_errtypes):
self.put(value)
def receive(self):
with as_deadlock(deadlock_errtypes):
return self.get()
class GeventBackend(Backend):
def shortname(self):
return 'gevent' # pragma: no cover
def start(self, func, *args, **kwargs):
grnlet = gevent.spawn(func, *args, **kwargs)
return grnlet
def run(self, func, *args, **kwargs):
grnlet = self.start(func, *args, **kwargs)
self.yield_()
return grnlet
def channel(self):
return Channel()
def yield_(self):
with as_deadlock(gevent.hub.LoopExit):
gevent.sleep()
def resume(self, tasklet):
self.yield_()
def propagate_exc(self, errtype, *args):
raise errtype
def would_deadlock(self):
# The Hub and main greenlet are always running,
# if there are more than those alive, we aren't going to deadlock.
count = 0
for obj in _gc.get_objects():
if isinstance(obj, greenlet.greenlet) and not obj.dead:
count += 1
if count > 2:
return False
return True
return GeventBackend()