Python eventlet.spawn() Examples

The following are 30 code examples of eventlet.spawn(). 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 eventlet , or try the search function .
Example #1
Source File: test.py    From signalslot with MIT License 6 votes vote down vote up
def test_semaphore(self, inspect):
        slot = mock.Mock()
        slot.side_effect = lambda **k: time.sleep(.3)

        signal = Signal('tost')
        signal.connect(slot)

        x = Task.get_or_create(signal, dict(some_kwarg='foo'),
                               logger=logging.getLogger('TaskX'))
        y = Task.get_or_create(signal, dict(some_kwarg='foo'),
                               logger=logging.getLogger('TaskY'))

        eventlet.spawn(x)
        time.sleep(.1)
        eventlet.spawn(y)
        time.sleep(.1)

        assert slot.call_count == 1
        time.sleep(.4)
        assert slot.call_count == 2 
Example #2
Source File: base_consumer.py    From distributed_framework with Apache License 2.0 6 votes vote down vote up
def schedulal_task_with_no_block(self):
        if ConsumersManager.schedual_task_always_use_thread:
            t = Thread(target=self.consumer.keep_circulating(1)(self.consumer._shedual_task))
            ConsumersManager.schedulal_thread_to_be_join.append(t)
            t.start()
        else:
            if self._concurrent_mode == 1:
                t = Thread(target=self.consumer.keep_circulating(1)(self.consumer._shedual_task))
                ConsumersManager.schedulal_thread_to_be_join.append(t)
                t.start()
            elif self._concurrent_mode == 2:
                g = gevent.spawn(self.consumer.keep_circulating(1)(self.consumer._shedual_task), )
                ConsumersManager.schedulal_thread_to_be_join.append(g)
            elif self._concurrent_mode == 3:
                g = eventlet.spawn(self.consumer.keep_circulating(1)(self.consumer._shedual_task), )
                ConsumersManager.schedulal_thread_to_be_join.append(g) 
Example #3
Source File: test_concurrency.py    From coveragepy-bbmirror with Apache License 2.0 6 votes vote down vote up
def test_bug_330(self):
        BUG_330 = """\
            from weakref import WeakKeyDictionary
            import eventlet

            def do():
                eventlet.sleep(.01)

            gts = WeakKeyDictionary()
            for _ in range(100):
                gts[eventlet.spawn(do)] = True
                eventlet.sleep(.005)

            eventlet.sleep(.1)
            print(len(gts))
            """
        self.try_some_code(BUG_330, "eventlet", eventlet, "0\n") 
Example #4
Source File: apollo_client.py    From pyapollo with Apache License 2.0 6 votes vote down vote up
def start(self, use_eventlet=False, eventlet_monkey_patch=False, catch_signals=True):
        # First do a blocking long poll to populate the local cache, otherwise we may get racing problems
        if len(self._cache) == 0:
            self._long_poll()
        if use_eventlet:
            import eventlet
            if eventlet_monkey_patch:
                eventlet.monkey_patch()
            eventlet.spawn(self._listener)
        else:
            if catch_signals:
                import signal
                signal.signal(signal.SIGINT, self._signal_handler)
                signal.signal(signal.SIGTERM, self._signal_handler)
                signal.signal(signal.SIGABRT, self._signal_handler)
            t = threading.Thread(target=self._listener)
            t.start() 
Example #5
Source File: test_taskflow_action_container.py    From watcher with Apache License 2.0 6 votes vote down vote up
def test_execute_with_cancel_action_plan(self, mock_eventlet_spawn):
        action_plan = obj_utils.create_test_action_plan(
            self.context, audit_id=self.audit.id,
            strategy_id=self.strategy.id,
            state=objects.action_plan.State.CANCELLING)

        action = obj_utils.create_test_action(
            self.context, action_plan_id=action_plan.id,
            state=objects.action.State.ONGOING,
            action_type='nop',
            input_parameters={'message': 'hello World'})
        action_container = tflow.TaskFlowActionContainer(
            db_action=action,
            engine=self.engine)

        def empty_test():
            pass
        et = eventlet.spawn(empty_test)
        mock_eventlet_spawn.return_value = et
        action_container.execute()
        et.kill.assert_called_with() 
Example #6
Source File: service.py    From oslo.service with Apache License 2.0 6 votes vote down vote up
def _child_wait_for_exit_or_signal(self, launcher):
        status = 0
        signo = 0

        # NOTE(johannes): All exceptions are caught to ensure this
        # doesn't fallback into the loop spawning children. It would
        # be bad for a child to spawn more children.
        try:
            launcher.wait()
        except SignalExit as exc:
            signame = self.signal_handler.signals_to_name[exc.signo]
            LOG.info('Child caught %s, handling', signame)
            status = exc.code
            signo = exc.signo
        except SystemExit as exc:
            launcher.stop()
            status = exc.code
        except BaseException:
            launcher.stop()
            LOG.exception('Unhandled exception')
            status = 2

        return status, signo 
Example #7
Source File: test.py    From plex-for-kodi with GNU General Public License v2.0 6 votes vote down vote up
def test_semaphore(self, inspect):
        slot = mock.Mock()
        slot.side_effect = lambda **k: time.sleep(.3)

        signal = Signal('tost')
        signal.connect(slot)

        x = Task.get_or_create(signal, dict(some_kwarg='foo'),
                               logger=logging.getLogger('TaskX'))
        y = Task.get_or_create(signal, dict(some_kwarg='foo'),
                               logger=logging.getLogger('TaskY'))

        eventlet.spawn(x)
        time.sleep(.1)
        eventlet.spawn(y)
        time.sleep(.1)

        assert slot.call_count == 1
        time.sleep(.4)
        assert slot.call_count == 2 
Example #8
Source File: geventlet.py    From jbox with MIT License 6 votes vote down vote up
def run(self):
        acceptors = []
        for sock in self.sockets:
            gsock = GreenSocket(sock)
            gsock.setblocking(1)
            hfun = partial(self.handle, gsock)
            acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
                                      self.worker_connections)

            acceptors.append(acceptor)
            eventlet.sleep(0.0)

        while self.alive:
            self.notify()
            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                [a.kill(eventlet.StopServe()) for a in acceptors]
                [a.wait() for a in acceptors]
        except eventlet.Timeout as te:
            if te != t:
                raise
            [a.kill() for a in acceptors] 
Example #9
Source File: geventlet.py    From jbox with MIT License 6 votes vote down vote up
def _eventlet_serve(sock, handle, concurrency):
    """
    Serve requests forever.

    This code is nearly identical to ``eventlet.convenience.serve`` except
    that it attempts to join the pool at the end, which allows for gunicorn
    graceful shutdowns.
    """
    pool = eventlet.greenpool.GreenPool(concurrency)
    server_gt = eventlet.greenthread.getcurrent()

    while True:
        try:
            conn, addr = sock.accept()
            gt = pool.spawn(handle, conn, addr)
            gt.link(_eventlet_stop, server_gt, conn)
            conn, addr, gt = None, None, None
        except eventlet.StopServe:
            sock.close()
            pool.waitall()
            return 
Example #10
Source File: hub.py    From ryu with Apache License 2.0 6 votes vote down vote up
def spawn(*args, **kwargs):
        def _launch(func, *args, **kwargs):
            # mimic gevent's default raise_error=False behaviour
            # by not propergating an exception to the joiner.
            try:
                func(*args, **kwargs)
            except greenlet.GreenletExit:
                pass
            except:
                # log uncaught exception.
                # note: this is an intentional divergence from gevent
                # behaviour.  gevent silently ignores such exceptions.
                LOG.error('hub: uncaught exception: %s',
                          traceback.format_exc())

        return eventlet.spawn(_launch, *args, **kwargs) 
Example #11
Source File: hub.py    From ryu with Apache License 2.0 6 votes vote down vote up
def __init__(self, listen_info, handle=None, backlog=None,
                     spawn='default', **ssl_args):
            assert backlog is None
            assert spawn == 'default'

            if ':' in listen_info[0]:
                self.server = eventlet.listen(listen_info,
                                              family=socket.AF_INET6)
            else:
                self.server = eventlet.listen(listen_info)
            if ssl_args:
                def wrap_and_handle(sock, addr):
                    ssl_args.setdefault('server_side', True)
                    handle(ssl.wrap_socket(sock, **ssl_args), addr)

                self.handle = wrap_and_handle
            else:
                self.handle = handle 
Example #12
Source File: cron.py    From coriolis with GNU Affero General Public License v3.0 6 votes vote down vote up
def _check_jobs(self):
        LOG.debug("Checking jobs")
        jobs = self._jobs.copy()
        job_nr = len(jobs)
        spawned = 0
        now = timeutils.utcnow()
        if job_nr:
            for job in jobs:
                if jobs[job].should_run(now):
                    LOG.debug("Spawning job %s" % job)
                    eventlet.spawn(jobs[job].start, now, self._queue)
                    spawned += 1

        done = timeutils.utcnow()
        delta = done - now
        LOG.debug("Spawned %(jobs)d jobs in %(seconds)d seconds" % {
            "seconds": delta.seconds,
            "jobs": spawned}) 
Example #13
Source File: utils.py    From masakari with Apache License 2.0 6 votes vote down vote up
def spawn_n(func, *args, **kwargs):
    """Passthrough method for eventlet.spawn_n.

    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.

    It will also grab the context from the threadlocal store and add it to
    the store on the new thread.  This allows for continuity in logging the
    context when using this method to spawn a new thread.
    """
    _context = common_context.get_current()

    @functools.wraps(func)
    def context_wrapper(*args, **kwargs):
        # NOTE: If update_store is not called after spawn_n it won't be
        # available for the logger to pull from threadlocal storage.
        if _context is not None:
            _context.update_store()
        func(*args, **kwargs)

    eventlet.spawn_n(context_wrapper, *args, **kwargs) 
Example #14
Source File: vif_pool.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def request_vif(self, pod, project_id, subnets, security_groups):
        try:
            host_addr = self._get_host_addr(pod)
        except KeyError:
            return None

        pool_key = self._get_pool_key(host_addr, project_id, None, subnets)

        try:
            return self._get_port_from_pool(pool_key, pod, subnets,
                                            tuple(sorted(security_groups)))
        except exceptions.ResourceNotReady:
            LOG.debug("Ports pool does not have available ports: %s",
                      pool_key)
            eventlet.spawn(self._populate_pool, pool_key, pod, subnets,
                           tuple(sorted(security_groups)))
            raise 
Example #15
Source File: utils.py    From masakari with Apache License 2.0 6 votes vote down vote up
def spawn(func, *args, **kwargs):
    """Passthrough method for eventlet.spawn.

    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.

    It will also grab the context from the threadlocal store and add it to
    the store on the new thread.  This allows for continuity in logging the
    context when using this method to spawn a new thread.
    """
    _context = common_context.get_current()

    @functools.wraps(func)
    def context_wrapper(*args, **kwargs):
        # NOTE: If update_store is not called after spawn it won't be
        # available for the logger to pull from threadlocal storage.
        if _context is not None:
            _context.update_store()
        return func(*args, **kwargs)

    return eventlet.spawn(context_wrapper, *args, **kwargs) 
Example #16
Source File: geventlet.py    From Flask-P2P with MIT License 6 votes vote down vote up
def _eventlet_serve(sock, handle, concurrency):
    """
    Serve requests forever.

    This code is nearly identical to ``eventlet.convenience.serve`` except
    that it attempts to join the pool at the end, which allows for gunicorn
    graceful shutdowns.
    """
    pool = eventlet.greenpool.GreenPool(concurrency)
    server_gt = eventlet.greenthread.getcurrent()

    while True:
        try:
            conn, addr = sock.accept()
            gt = pool.spawn(handle, conn, addr)
            gt.link(_eventlet_stop, server_gt, conn)
            conn, addr, gt = None, None, None
        except eventlet.StopServe:
            pool.waitall()
            return 
Example #17
Source File: geventlet.py    From Flask-P2P with MIT License 6 votes vote down vote up
def run(self):
        acceptors = []
        for sock in self.sockets:
            gsock = GreenSocket(sock)
            gsock.setblocking(1)
            hfun = partial(self.handle, gsock)
            acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
                                      self.worker_connections)

            acceptors.append(acceptor)
            eventlet.sleep(0.0)

        while self.alive:
            self.notify()
            eventlet.sleep(1.0)

        self.notify()
        try:
            with eventlet.Timeout(self.cfg.graceful_timeout) as t:
                [a.kill(eventlet.StopServe()) for a in acceptors]
                [a.wait() for a in acceptors]
        except eventlet.Timeout as te:
            if te != t:
                raise
            [a.kill() for a in acceptors] 
Example #18
Source File: test_concurrency.py    From coveragepy with Apache License 2.0 6 votes vote down vote up
def test_bug_330(self):
        BUG_330 = """\
            from weakref import WeakKeyDictionary
            import eventlet

            def do():
                eventlet.sleep(.01)

            gts = WeakKeyDictionary()
            for _ in range(100):
                gts[eventlet.spawn(do)] = True
                eventlet.sleep(.005)

            eventlet.sleep(.1)
            print(len(gts))
            """
        self.try_some_code(BUG_330, "eventlet", eventlet, "0\n") 
Example #19
Source File: wsgi.py    From oslo.service with Apache License 2.0 5 votes vote down vote up
def start(self):
        """Start serving a WSGI application.

        :returns: None
        """
        # The server socket object will be closed after server exits,
        # but the underlying file descriptor will remain open, and will
        # give bad file descriptor error. So duplicating the socket object,
        # to keep file descriptor usable.

        self.dup_socket = self.socket.dup()

        if self._use_ssl:
            self.dup_socket = sslutils.wrap(self.conf, self.dup_socket)

        wsgi_kwargs = {
            'func': eventlet.wsgi.server,
            'sock': self.dup_socket,
            'site': self.app,
            'protocol': self._protocol,
            'custom_pool': self._pool,
            'log': self._logger,
            'log_format': self.conf.wsgi_log_format,
            'debug': False,
            'keepalive': self.conf.wsgi_keep_alive,
            'socket_timeout': self.client_socket_timeout
            }

        if self._max_url_len:
            wsgi_kwargs['url_length_limit'] = self._max_url_len

        self._server = eventlet.spawn(**wsgi_kwargs) 
Example #20
Source File: green_thread_executor.py    From karbor with Apache License 2.0 5 votes vote down vote up
def _create_thread(self, function, operation_id, param):
        gt = eventlet.spawn(function, operation_id, param)
        self._operation_thread_map[operation_id] = gt
        gt.link(self._on_gt_done, operation_id) 
Example #21
Source File: backup_writers.py    From coriolis with GNU Affero General Public License v3.0 5 votes vote down vote up
def _open(self):
        self._exec_helper_cmd()
        self._sender_evt = eventlet.spawn(
            self._sender)
        for _ in range(self._encoder_cnt):
            self._encoder_evt.append(
                eventlet.spawn(self._encoder)) 
Example #22
Source File: cron.py    From coriolis with GNU Affero General Public License v3.0 5 votes vote down vote up
def start(self):
        schedule.every().minute.do(self._check_jobs)
        self._eventlets.append(eventlet.spawn(self._loop))
        self._eventlets.append(eventlet.spawn(self._janitor))
        self._eventlets.append(eventlet.spawn(self._result_loop))
        eventlet.spawn(self._ripper) 
Example #23
Source File: test_nameko_sentry.py    From nameko-sentry with Apache License 2.0 5 votes vote down vote up
def test_concurrent_workers(
        self, container_factory, config, web_session
    ):

        class Service(object):
            name = "service"

            sentry = SentryReporter()

            @http('GET', '/resource')
            def resource(self, request):
                raise CustomException()

        container = container_factory(Service, config)
        container.start()

        called = Mock()

        def called_twice(worker_ctx, res, exc_info):
            called()
            return called.call_count == 2

        with entrypoint_waiter(container, 'resource', callback=called_twice):
            eventlet.spawn(web_session.get, '/resource?q1')
            eventlet.spawn(web_session.get, '/resource?q2')

        sentry = get_extension(container, SentryReporter)

        assert sentry.client.send.call_count == 2
        query_strings = {
            kwargs['request']['query_string']
            for (_, kwargs) in sentry.client.send.call_args_list
        }
        assert query_strings == {"q1", "q2"} 
Example #24
Source File: _app.py    From bowtie with MIT License 5 votes vote down vote up
def start(self):
        """Start the scheduled task."""
        self.thread = eventlet.spawn(self.run) 
Example #25
Source File: backup_writers.py    From coriolis with GNU Affero General Public License v3.0 5 votes vote down vote up
def _open(self):
        self._closing = False
        self._init_session()
        self._acquire()
        self._sender_evt = eventlet.spawn(self._sender)
        if self._compressor_count is None or self._compressor_count == 0:
            self._compressor_count = 1
        self._compressor_evt = []
        for _ in range(self._compressor_count):
            self._compressor_evt.append(
                eventlet.spawn(self._compressor)) 
Example #26
Source File: dumper.py    From st2 with Apache License 2.0 5 votes vote down vote up
def start(self, wait=False):
        self._flush_thread = eventlet.spawn(self._flush)
        if wait:
            self.wait() 
Example #27
Source File: worker.py    From st2 with Apache License 2.0 5 votes vote down vote up
def start(self, wait=False):
        LOG.info('Bootstrapping executions from db...')
        try:
            self._bootstrap()
        except:
            LOG.exception('Unable to bootstrap executions from db. Aborting.')
            raise
        self._consumer_thread = eventlet.spawn(super(ExecutionsExporter, self).start, wait=True)
        self._dumper.start()
        if wait:
            self.wait() 
Example #28
Source File: service.py    From oslo.service with Apache License 2.0 5 votes vote down vote up
def _handle_signal(self, signo, frame):
        # This method can be called anytime, even between two Python
        # instructions. It's scheduled by the C signal handler of Python using
        # Py_AddPendingCall().
        #
        # We only do one thing: schedule a call to _handle_signal_cb() later.
        # eventlet.spawn() is not signal-safe: _handle_signal() can be called
        # during a call to eventlet.spawn(). This case is supported, it is
        # ok to schedule multiple calls to _handle_signal() with the same
        # signal number.
        #
        # To call to _handle_signal_cb() is delayed to avoid reentrant calls to
        # _handle_signal_cb(). It avoids race conditions like reentrant call to
        # clear(): clear() is not reentrant (bug #1538204).
        eventlet.spawn(self._handle_signal_cb, signo, frame)

        # On Python >= 3.5, ensure that eventlet's poll() or sleep() call is
        # interrupted by raising an exception. If the signal handler does not
        # raise an exception then due to PEP 475 the call will not return until
        # an event is detected on a file descriptor or the timeout is reached,
        # and thus eventlet will not wake up and notice that there has been a
        # new thread spawned.
        if self.__force_interrupt_on_signal:
            try:
                interrupted_frame = inspect.stack(context=0)[1]
            except IndexError:
                pass
            else:
                if ((interrupted_frame.function == 'do_poll' and
                     interrupted_frame.filename == self.__hub_module_file) or
                    (interrupted_frame.function == 'do_sleep' and
                     interrupted_frame.filename == __file__)):
                    raise IOError(errno.EINTR, 'Interrupted') 
Example #29
Source File: test_periodics.py    From futurist with Apache License 2.0 5 votes vote down vote up
def create_destroy_green_thread(run_what, *args, **kwargs):
    t = eventlet.spawn(run_what, *args, **kwargs)
    try:
        yield
    finally:
        t.wait() 
Example #30
Source File: _app.py    From bowtie with MIT License 5 votes vote down vote up
def run(self):
        """Invoke the function repeatedly on a timer."""
        ret = eventlet.spawn(self.context(self.func))
        eventlet.sleep(self.seconds)
        try:
            ret.wait()
        except Exception:  # pylint: disable=broad-except
            traceback.print_exc()
        self.thread = eventlet.spawn(self.run)