Python gevent.hub() Examples

The following are 6 code examples of gevent.hub(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gevent , or try the search function .
Example #1
Source File: main.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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) 
Example #2
Source File: runtime.py    From pypkjs with MIT License 5 votes vote down vote up
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.") 
Example #3
Source File: instrumentation.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
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() 
Example #4
Source File: instrumentation.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
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
        } 
Example #5
Source File: instrumentation.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #6
Source File: backends.py    From goless with Apache License 2.0 4 votes vote down vote up
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()