Python gevent.spawn_later() Examples

The following are 30 code examples of gevent.spawn_later(). 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: videocall.py    From janus-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def handle_incoming_call(self, caller_username, backend_server_url):
        if self.videocall_user is None:
            raise JanusCloudError('Register a username first', JANUS_VIDEOCALL_ERROR_REGISTER_FIRST)
        if self.videocall_user.incall:
            raise JanusCloudError('User {} busy'.format(self.videocall_user.username), JANUS_VIDEOCALL_ERROR_ALREADY_IN_CALL)
        self.videocall_user.incall = True
        self.videocall_user.peer_name = caller_username
        self.videocall_user.utime = time.time()
        try:
            self._connect_backend(backend_server_url)
            self._plugin.user_dao.update(self.videocall_user)
        except Exception:
            self._disconnect_backend()
            self.videocall_user.peer_name = ''
            self.videocall_user.incall = False
            raise

        # if incoming_call event cannot be received in INCOMMING_CALL_TIMEOUT(10) seconds,
        # auto disconnect the backend server
        if self._auto_disconnect_greenlet is None:
            self._auto_disconnect_greenlet = gevent.spawn_later(INCOMMING_CALL_TIMEOUT, self._auto_disconnect_routine) 
Example #2
Source File: consumer.py    From gnsq with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle_connection_failure(self, conn):
        del self._connections[conn]
        conn.close_stream()

        if not self.is_running:
            return

        self.redistribute_ready_state()

        if str(conn) not in self.nsqd_tcp_addresses:
            return

        seconds = self._connection_backoffs[conn].failure().get_interval()
        self.logger.debug('[%s] retrying in %ss', conn, seconds)

        gevent.spawn_later(
            seconds, self.connect_to_nsqd, conn.address, conn.port) 
Example #3
Source File: consumer.py    From gnsq with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _start_backoff(self, conn):
        self._connections[conn] = BACKOFF

        interval = self._message_backoffs[conn].get_interval()
        gevent.spawn_later(interval, self._start_throttled, conn)

        self.logger.info('[%s] backing off for %s seconds', conn, interval)
        self.redistribute_ready_state() 
Example #4
Source File: gevent_impl.py    From haven with MIT License 6 votes vote down vote up
def set(self, interval, callback, repeat=False, force=True):
        """
        添加timer
        """
        if self.timer:
            if force:
                # 如果已经存在,那么先要把现在的清空
                self.clear()
            else:
                # 已经存在的话,就返回了
                return

        def callback_wrapper():
            # 必须要确定,这次调用就是这个timer引起的
            if self.timer == timer:
                # 必须加这句,否则如果在callback中有clear操作,会出现GreenletExit
                self.timer = None
                # 不可以加 timer = None,否则会导致判断self.timer == timer 报错找不到timer
                result = safe_call(callback)
                if repeat and not self.timer:
                    # 之所以还要判断timer,是因为callback中可能设置了新的回调
                    self.set(interval, callback, repeat, True)
                return result

        self.timer = timer = gevent.spawn_later(interval, callback_wrapper) 
Example #5
Source File: __init__.py    From OpenMTC with Eclipse Public License 1.0 6 votes vote down vote up
def set_timer(self, t, f, *args, **kw):
        timer = None

        def wrapper():
            self._timers.discard(timer)
            f(*args, **kw)

        timer = spawn_later(t, wrapper)
        self._timers.add(timer)
        return timer 
Example #6
Source File: statusapi.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def _clean_local_backup(local_backup_file, g):
    def _safe_remove(path):
        LOG.info('Removing backup {}'.format(local_backup_file))
        try:
            os.remove(path)
        except:
            pass

    if g.value != 0:
        _safe_remove(local_backup_file)
        return

    LOG.info('Removing backup {} in 300s'.format(local_backup_file))
    gevent.spawn_later(300, _safe_remove, local_backup_file)


# XXX this should be moved to a different endpoint 
Example #7
Source File: backend_session.py    From janus-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def _keepalive_routine(self):
        gevent.sleep(self._keepalive_interval)
        keepalive_msg = create_janus_msg('keepalive')
        while self.state == BACKEND_SESSION_STATE_ACTIVE:
            try:
                # if there is no handle existed and auto destroy is enabled, just schedule the destroy route
                if not self._handles:
                    if self._auto_destroy and self._auto_destroy_greenlet is None:
                        self._auto_destroy_greenlet = gevent.spawn_later(self._auto_destroy, self._auto_destroy_routine)

                self.send_request(keepalive_msg, ignore_ack=False)

            except Exception as e:
                log.exception('Keepalive failed for backend session {}'.format(self.url))
                self.destroy()
            else:
                gevent.sleep(self._keepalive_interval) 
Example #8
Source File: iamrolemanager.py    From confidant with Apache License 2.0 6 votes vote down vote up
def refresh_cache():
    global ROLES
    refresh_rate = settings.BACKGROUND_CACHE_IAM_ROLE_REFRESH_RATE
    if settings.BACKGROUND_CACHE_IAM_ROLE_REFRESH_RATE < 60:
        refresh_rate = 60
    try:
        logger.info('Refreshing IAM roles cache.')
        ROLES = _get_iam_roles()
    except Exception:
        logger.exception(
            'Failed to update IAM roles cache.',
            exc_info=True
        )
    finally:
        # +/- 20ish seconds for respawn, to ensure all processes do not
        # refresh at the same time
        random_refresh_rate = random.randrange(
            refresh_rate - 20,
            refresh_rate + 20
        )
        return gevent.spawn_later(
            random_refresh_rate,
            refresh_cache
        ) 
Example #9
Source File: utils.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def __call__(self, *args, **kwargs):
        now = utc_millisec()
        remaining = self.wait - (now - self._previous)

        if self._cancelled:
            return

        if remaining <= 0 or remaining > self.wait:
            if self._timeout is not None:
                self._timeout.join(timeout=5)
                self._timeout = None
            self._previous = now
            self.f(*args, **kwargs)

        elif self._timeout is None:
            self._args = args
            self._kwargs = kwargs
            self._timeout = gevent.spawn_later(remaining/1000.0, self.later)

        else:
            self._args = args
            self._kwargs = kwargs 
Example #10
Source File: basepoller.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def start(self):
        super(BasePollerFT, self).start()

        if self._actor_glet is not None:
            return

        self._actor_glet = gevent.spawn(
            self._actor_loop
        )
        self._poll_glet = gevent.spawn_later(
            random.randint(0, 2),
            self._poll_loop
        )

        self._age_out_glet = gevent.spawn(
            self._age_out_loop
        ) 
Example #11
Source File: util.py    From aurproxy with Apache License 2.0 5 votes vote down vote up
def _run(self):
    if not self._stop_event.isSet():
      try:
        self._fn()
      except Exception:
        logger = get_logger(unicode(self._fn))
        logger.exception("Failed to execute PeriodicTask.")
      finally:
        gevent.spawn_later(self._period, self._run) 
Example #12
Source File: __init__.py    From python-sensor with MIT License 5 votes vote down vote up
def boot_agent_later():
    """ Executes <boot_agent> in the future! """
    if 'gevent' in sys.modules:
        import gevent
        gevent.spawn_later(2.0, boot_agent)
    else:
        t = Timer(2.0, boot_agent)
        t.start() 
Example #13
Source File: test_socket.py    From pySINDy with MIT License 5 votes vote down vote up
def test_timeout(self):
            a,b = self.create_bound_pair()
            g = gevent.spawn_later(0.5, lambda: a.send(b'hi'))
            timeout = gevent.Timeout(0.1)
            timeout.start()
            self.assertRaises(gevent.Timeout, b.recv)
            g.kill() 
Example #14
Source File: manager.py    From python-socketio-client with MIT License 5 votes vote down vote up
def start_task(self, func, delay=0, *args, **kwargs):
        return gevent.spawn_later(delay, func, *args, **kwargs) 
Example #15
Source File: connection.py    From channelstream with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, username, conn_id):
        self.username = username  # hold user id/name of connection
        self.last_active = None
        self.socket = None
        self.queue = None
        self.id = conn_id
        self.mark_activity()
        gevent.spawn_later(5, self.heartbeat_forever) 
Example #16
Source File: util.py    From aurproxy with Apache License 2.0 5 votes vote down vote up
def start(self):
    """Override of base method.
    """
    gevent.spawn_later(self._period, self._run) 
Example #17
Source File: __init__.py    From Eel with MIT License 5 votes vote down vote up
def _websocket_close(page):
    global _shutdown

    close_callback = _start_args.get('close_callback')

    if close_callback is not None:
        sockets = [p for _, p in _websockets]
        close_callback(page, sockets)
    else:
        if _shutdown:
            _shutdown.kill()

        _shutdown = gvt.spawn_later(1.0, _detect_shutdown) 
Example #18
Source File: run.py    From psdash with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def _setup_workers(self):
        net_io_interval = self.app.config.get('PSDASH_NET_IO_COUNTER_INTERVAL', self.DEFAULT_NET_IO_COUNTER_INTERVAL)
        gevent.spawn_later(net_io_interval, self._net_io_counters_worker, net_io_interval)

        if 'PSDASH_LOGS' in self.app.config:
            logs_interval = self.app.config.get('PSDASH_LOGS_INTERVAL', self.DEFAULT_LOG_INTERVAL)
            gevent.spawn_later(logs_interval, self._logs_worker, logs_interval)

        if self.app.config.get('PSDASH_AGENT'):
            register_interval = self.app.config.get('PSDASH_REGISTER_INTERVAL', self.DEFAULT_REGISTER_INTERVAL)
            gevent.spawn_later(register_interval, self._register_agent_worker, register_interval) 
Example #19
Source File: geventwrapper.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def gevent_spawn_later(task_name: str, seconds, func, *args, **kwargs):

    def wrapper_func(*args, **kwargs):
        logger = logging.getLogger('gevent_spawn_later')
        gevent.getcurrent().name = task_name

        try:
            return func(*args, **kwargs)
        except Exception as e:
            logger.exception('%s greenlet terminated with an unhandled exception:' % task_name, exc_info=e)
            raise

    return gevent.spawn_later(seconds, wrapper_func, *args, **kwargs) 
Example #20
Source File: authbot.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def send_and_schedule_keepalive_message(self):
        if self.hirez_login_server and self.display_name:
            self.hirez_login_server.send(
                a01c8().set([
                    m068b().set([])
                ])
            )
        gevent.spawn_later(30, self.send_and_schedule_keepalive_message) 
Example #21
Source File: test_socket.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_timeout(self):
            a,b = self.create_bound_pair()
            g = gevent.spawn_later(0.5, lambda: a.send(b'hi'))
            timeout = gevent.Timeout(0.1)
            timeout.start()
            self.assertRaises(gevent.Timeout, b.recv)
            g.kill() 
Example #22
Source File: test_gevent_pool.py    From mars with Apache License 2.0 5 votes vote down vote up
def testPoolJoin(self):
        with create_actor_pool(address=True, n_process=2, distributor=AdminDistributor(2),
                               backend='gevent') as pool:
            start = time.time()
            pool.join(0.2)
            self.assertGreaterEqual(time.time() - start, 0.2)

            start = time.time()
            gevent.spawn_later(0.2, lambda: pool.stop())
            pool.join()
            self.assertGreaterEqual(time.time() - start, 0.2) 
Example #23
Source File: syslog.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(SyslogMiner, self).start()

        if self.amqp_glet is not None:
            return

        self.amqp_glet = gevent.spawn_later(
            random.randint(0, 2),
            self._amqp_consumer
        )
        self.ageout_glet = gevent.spawn(self._age_out_loop)
        self._actor_glet = gevent.spawn(self._actor_loop) 
Example #24
Source File: syslog.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(SyslogMatcher, self).start()

        self.amqp_glet = gevent.spawn_later(
            2,
            self._amqp_consumer
        ) 
Example #25
Source File: xmpp.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(XMPPMiner, self).start()

        if self._xmpp_glet is not None:
            return
        self._xmpp_glet = gevent.spawn_later(random.randint(0, 2), self._run) 
Example #26
Source File: xmpp.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(XMPPOutput, self).start()

        if self._xmpp_glet is not None:
            return
        self._xmpp_glet = gevent.spawn_later(random.randint(0, 2), self._run)
        self._publisher_glet = gevent.spawn_later(random.randint(0, 3), self._publisher) 
Example #27
Source File: test.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(TestMiner, self).start()

        self._glet = gevent.spawn_later(
            2,
            self._run
        ) 
Example #28
Source File: dag_ng.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(DagPusher, self).start()

        if self.device_list_glet is not None:
            return

        self.device_list_glet = gevent.spawn_later(
            2,
            self._device_list_monitor
        )

        if self.age_out_interval is not None:
            self.ageout_glet = gevent.spawn(self._age_out_run) 
Example #29
Source File: panos.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        super(PanOSLogsAPIFT, self).start()

        if self.glet is not None:
            return

        self.glet = gevent.spawn_later(random.randint(0, 2), self._run)

        for idx in range(len(self.fields)):
            self.age_out_glets.append(
                gevent.spawn(self._age_out_loop, idx)
            ) 
Example #30
Source File: producer.py    From gnsq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def handle_connection_failure(self, conn):
        conn.close_stream()
        self._clear_responses(conn, NSQException('connection closed'))

        if not self.is_running:
            return

        seconds = self._connection_backoffs[conn].failure().get_interval()
        self.logger.debug('[%s] retrying in %ss', conn, seconds)

        gevent.spawn_later(
            seconds, self.connect_to_nsqd, conn.address, conn.port)