def _ConsumeMessages(message_q, setup_args, setup_kwargs): """Configures ts_mon and gets metrics from a message queue. Args: message_q: A multiprocessing.Queue to read metrics from. setup_args: Arguments to pass to SetupTsMonGlobalState. setup_kwargs: Keyword arguments to pass to SetupTsMonGlobalState. """ last_flush = 0 pending = False # If our parent dies, finish flushing before exiting. reset_after = [] parallel.ExitWithParent(signal.SIGHUP) signal.signal(signal.SIGHUP, lambda _sig, _stack: _WaitToFlush(last_flush, reset_after=reset_after)) # Configure ts-mon, but don't start up a sending thread. setup_kwargs['auto_flush'] = False SetupTsMonGlobalState(*setup_args, **setup_kwargs) message = message_q.get() while message: try: cls = getattr(metrics, message.metric_name) metric = cls(*message.metric_args, **message.metric_kwargs) if message.reset_after: reset_after.append(metric) getattr(metric, message.method)( *message.method_args, **message.method_kwargs) except Exception: logging.exception('Caught an exception while running %s', _MethodCallRepr(message.metric_name, message.method, message.method_args, message.method_kwargs)) pending, last_flush, time_delta = _FlushIfReady(True, last_flush, reset_after=reset_after) try: # Only wait until the next flush time if we have pending metrics. timeout = FLUSH_INTERVAL - time_delta if pending else None message = message_q.get(timeout=timeout) except Queue.Empty: # We had pending metrics, but we didn't get a new message. Flush and wait # indefinitely. pending, last_flush, _ = _FlushIfReady(pending, last_flush, reset_after=reset_after) # Wait as long as we need to for the next metric. message = message_q.get() if pending: _WaitToFlush(last_flush, reset_after=reset_after)
def __init__(self, message_q): # If our parent dies, finish flushing before exiting. self.reset_after_flush = [] self.last_flush = 0 self.pending = False self.message_q = message_q if parallel.ExitWithParent(signal.SIGHUP): signal.signal(signal.SIGHUP, lambda _sig, _stack: self._WaitToFlush())
def GrandChild(): parallel.ExitWithParent() time.sleep(9)