Python eventlet.GreenPool() Examples

The following are 24 code examples of eventlet.GreenPool(). 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: wsgi.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def get_asynchronous_eventlet_pool(size=1000):
    """Return eventlet pool to caller.

    Also store pools created in global list, to wait on
    it after getting signal for graceful shutdown.

    :param size: eventlet pool size
    :returns: eventlet pool
    """
    global ASYNC_EVENTLET_THREAD_POOL_LIST

    pool = eventlet.GreenPool(size=size)
    # Add pool to global ASYNC_EVENTLET_THREAD_POOL_LIST
    ASYNC_EVENTLET_THREAD_POOL_LIST.append(pool)

    return pool 
Example #2
Source File: wsgi.py    From senlin with Apache License 2.0 6 votes vote down vote up
def run_server(self):
        """Run a WSGI server."""

        eventlet.wsgi.HttpProtocol.default_request_version = "HTTP/1.0"
        eventlet.hubs.use_hub('poll')
        eventlet.patcher.monkey_patch(all=False, socket=True)
        self.pool = eventlet.GreenPool(size=self.threads)
        socket_timeout = cfg.CONF.senlin_api.client_socket_timeout or None

        try:
            eventlet.wsgi.server(
                self.sock, self.application,
                custom_pool=self.pool,
                url_length_limit=URL_LENGTH_LIMIT,
                log=self._logger,
                debug=cfg.CONF.debug,
                keepalive=cfg.CONF.senlin_api.wsgi_keep_alive,
                socket_timeout=socket_timeout)
        except socket.error as err:
            if err[0] != errno.EINVAL:
                raise

        self.pool.waitall() 
Example #3
Source File: plugin.py    From tacker with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        super(VNFMPlugin, self).__init__()
        self._pool = eventlet.GreenPool()
        self.boot_wait = cfg.CONF.tacker.boot_wait
        self.vim_client = vim_client.VimClient()
        self._vnf_manager = driver_manager.DriverManager(
            'tacker.tacker.vnfm.drivers',
            cfg.CONF.tacker.infra_driver)
        self._vnf_action = driver_manager.DriverManager(
            'tacker.tacker.policy.actions',
            cfg.CONF.tacker.policy_action)
        self._vnf_monitor = monitor.VNFMonitor(self.boot_wait)
        self._vnf_alarm_monitor = monitor.VNFAlarmMonitor()
        self._vnf_reservation_monitor = monitor.VNFReservationAlarmMonitor()
        self._vnf_app_monitor = monitor.VNFAppMonitor()
        self._init_monitoring() 
Example #4
Source File: api.py    From st2 with Apache License 2.0 6 votes vote down vote up
def _run_server():
    host = cfg.CONF.stream.host
    port = cfg.CONF.stream.port

    LOG.info('(PID=%s) ST2 Stream API is serving on http://%s:%s.', os.getpid(), host, port)

    max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
    worker_pool = eventlet.GreenPool(max_pool_size)
    sock = eventlet.listen((host, port))

    def queue_shutdown(signal_number, stack_frame):
        eventlet.spawn_n(shutdown_server_kill_pending_requests, sock=sock,
                         worker_pool=worker_pool, wait_time=WSGI_SERVER_REQUEST_SHUTDOWN_TIME)

    # We register a custom SIGINT handler which allows us to kill long running active requests.
    # Note: Eventually we will support draining (waiting for short-running requests), but we
    # will still want to kill long running stream requests.
    register_stream_signal_handlers(handler_func=queue_shutdown)

    wsgi.server(sock, app.setup_app(), custom_pool=worker_pool)
    return 0 
Example #5
Source File: test_detox.py    From detox with MIT License 6 votes vote down vote up
def test_runtestmulti(self):
        class MyConfig:
            class MyOption:
                numproc = 7

            option = MyOption()

        x = []

        def MyGreenPool(**kw):
            x.append(kw)
            # Building a Detox object will already call GreenPool(),
            # so we have to let MyGreenPool being called twice before raise
            if len(x) == 2:
                raise ValueError

        from detox import proc

        setattr(proc, "GreenPool", MyGreenPool)
        with pytest.raises(ValueError):
            d = proc.Detox(MyConfig())
            d.runtestsmulti(["env1", "env2", "env3"])  # Fake env list

        assert x[0] == {}  # When building Detox object
        assert x[1] == {"size": 7}  # When calling runtestsmulti 
Example #6
Source File: vxrd.py    From vxfld with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, conf):
        super(_Vxrd, self).__init__(conf)
        self.__vni_config = {}    # vni_config[vni] = DeviceConfig
        self.__peerdb = {}        # peerdb[vni] = {ip, ...}
        self.__next_refresh = 0
        self.__next_sync_check = 0
        self.__nlmonitor = netlink.Netlink(
            {netlink.Netlink.NEWLINK: self.__add_iface,
             netlink.Netlink.DELLINK: self.__del_iface},
            self._logger,
            pool=self._pool
        )
        # socket for RD-SND communication.
        self.__sockpool = eventlet.pools.Pool(max_size=1)
        # Send a message to the SND and flush bridge fdb entries when the
        # process exits.
        atexit.register(self.__remove_vnis)
        if self._conf.head_rep:
            # HER: The RD limits the pool size to 1 to avoid parallel updates
            # to the kernel state.
            self.__herpool = eventlet.GreenPool(size=1)
            # Used to keep track of when the last response was received from
            # the SND.
            self.__last_response = None
            # Frequency at which the kernel state should be synced with the
            # peerdb.
            self.__sync_ready = True 
Example #7
Source File: eventlet.py    From python-pool-performance with MIT License 5 votes vote down vote up
def init_pool(self, worker_count):
        return eventlet.GreenPool(worker_count) 
Example #8
Source File: service.py    From vxfld with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, conf):
        self._conf = conf
        # Set up signal handlers before daemonizing.
        self.__setup_signal_handlers()
        self.__daemon_context = daemon.DaemonContext(
            working_directory='/var/run'
        )
        if self._conf.daemon:
            self.__daemon_context.open()
        # Patch system modules to be greenthread-friendly. Must be after
        # daemonizing.
        eventlet.monkey_patch()
        self._pool = eventlet.GreenPool(size=self._conf.concurrency)
        self.__run_gt = None
        # Set up logging. Must be after daemonizing.
        if self._conf.debug:
            lvl = logging.DEBUG
            self.__spawn_backdoor_server()
        else:
            lvl = getattr(logging, self._conf.loglevel)
        if self._conf.logdest in {LogDestination.SYSLOG,
                                  LogDestination.STDOUT}:
            self._logger = utils.get_logger(self._conf.node_type,
                                            self._conf.logdest)
        else:
            self._logger = utils.get_logger(
                self._conf.node_type,
                LogDestination.LOGFILE,
                filehandler_args={'filename': self._conf.logdest,
                                  'maxBytes': self._conf.logfilesize,
                                  'backupCount': self._conf.logbackupcount}
            )
        self._logger.setLevel(lvl)
        self._mgmtserver = mgmt.MgmtServer(self._conf.udsfile,
                                           self._process,
                                           self._logger)
        self.__pidfd = utils.Pidfile(self._conf.pidfile,
                                     self._conf.node_type,
                                     uuid=self._conf.node_id) 
Example #9
Source File: netlink.py    From vxfld with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, process_cb, logger, pool=None):
        self.__process_cbs = process_cb
        self.__logger = logger
        self.__pool = pool or eventlet.GreenPool()
        self.__intfevent = None
        self.__intfqueue = None
        self.__running = set()
        self.dispatcher_gt = None
        self.socket = None 
Example #10
Source File: wsgi.py    From senlin with Apache License 2.0 5 votes vote down vote up
def start_wsgi(self):
        if self.conf.workers == 0:
            # Useful for profiling, test, debug etc.
            self.pool = eventlet.GreenPool(size=self.threads)
            self.pool.spawn_n(self._single_run, self.application, self.sock)
            return

        LOG.info("Starting %d workers", self.conf.workers)
        signal.signal(signal.SIGTERM, self.kill_children)
        signal.signal(signal.SIGINT, self.kill_children)
        signal.signal(signal.SIGHUP, self.hup)
        while len(self.children) < self.conf.workers:
            self.run_child() 
Example #11
Source File: test_detox.py    From detox with MIT License 5 votes vote down vote up
def test_getresources_parallel(self):
        x = []

        class Provider:
            def provide_abc(self):
                x.append(1)
                return 42

        resources = Resources(Provider())
        pool = eventlet.GreenPool(2)
        pool.spawn(lambda: resources.getresources("abc"))
        pool.spawn(lambda: resources.getresources("abc"))
        pool.waitall()
        assert len(x) == 1 
Example #12
Source File: proc.py    From detox with MIT License 5 votes vote down vote up
def __init__(self, providerbase):
        self._providerbase = providerbase
        self._spec2thread = {}
        self._pool = GreenPool()
        self._resources = {} 
Example #13
Source File: proc.py    From detox with MIT License 5 votes vote down vote up
def runtestsmulti(self, envlist):
        pool = GreenPool(size=self._toxconfig.option.numproc)
        threads = []
        for env in envlist:
            threads.append(pool.spawn(self.runtests, env))

        for t in threads:
            # re-raises any exceptions of the worker thread
            t.wait()
        if not self.toxsession.config.option.sdistonly:
            retcode = self._toxsession._summary()
            return retcode 
Example #14
Source File: api.py    From st2 with Apache License 2.0 5 votes vote down vote up
def _run_server():
    host = cfg.CONF.api.host
    port = cfg.CONF.api.port

    LOG.info('(PID=%s) ST2 API is serving on http://%s:%s.', os.getpid(), host, port)

    max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
    worker_pool = eventlet.GreenPool(max_pool_size)
    sock = eventlet.listen((host, port))

    wsgi.server(sock, app.setup_app(), custom_pool=worker_pool, log=LOG, log_output=False)
    return 0 
Example #15
Source File: greenpooldispatch.py    From st2 with Apache License 2.0 5 votes vote down vote up
def __init__(self, dispatch_pool_size=50, monitor_thread_empty_q_sleep_time=5,
                 monitor_thread_no_workers_sleep_time=1, name=None):
        self._pool_limit = dispatch_pool_size
        self._dispatcher_pool = eventlet.GreenPool(dispatch_pool_size)
        self._dispatch_monitor_thread = eventlet.greenthread.spawn(self._flush)
        self._monitor_thread_empty_q_sleep_time = monitor_thread_empty_q_sleep_time
        self._monitor_thread_no_workers_sleep_time = monitor_thread_no_workers_sleep_time
        self._name = name

        self._work_buffer = six.moves.queue.Queue()

        # Internal attributes we use to track how long the pool is busy without any free workers
        self._pool_last_free_ts = time.time() 
Example #16
Source File: test_scheduler_retry.py    From st2 with Apache License 2.0 5 votes vote down vote up
def test_handler_retry_unexpected_error(self):
        scheduling_queue_handler = handler.ActionExecutionSchedulingQueueHandler()
        self.assertRaises(KeyError, scheduling_queue_handler.process)
        self.assertEqual(eventlet.GreenPool.spawn.call_count, 0) 
Example #17
Source File: test_scheduler_retry.py    From st2 with Apache License 2.0 5 votes vote down vote up
def test_handler_retries_exhausted(self):
        scheduling_queue_handler = handler.ActionExecutionSchedulingQueueHandler()
        self.assertRaises(pymongo.errors.ConnectionFailure, scheduling_queue_handler.process)
        self.assertEqual(eventlet.GreenPool.spawn.call_count, 0) 
Example #18
Source File: test_scheduler_retry.py    From st2 with Apache License 2.0 5 votes vote down vote up
def test_handler_retry_connection_error(self):
        scheduling_queue_handler = handler.ActionExecutionSchedulingQueueHandler()
        scheduling_queue_handler.process()

        # Make sure retry occurs and that _handle_execution in process is called.
        calls = [mock.call(scheduling_queue_handler._handle_execution, MOCK_QUEUE_ITEM)]
        eventlet.GreenPool.spawn.assert_has_calls(calls) 
Example #19
Source File: handler.py    From st2 with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.message_type = LiveActionDB
        self._shutdown = False
        self._pool = eventlet.GreenPool(size=cfg.CONF.scheduler.pool_size)
        # If an ActionExecutionSchedulingQueueItemDB object hasn't been updated fore more than
        # this amount of milliseconds, it will be marked as "handled=False".
        # As soon as an item is picked by scheduler to be processed, it should be processed very
        # fast (< 5 seconds). If an item is still being marked as processing it likely indicates
        # that the scheduler process which was processing that item crashed or similar so we need
        # to mark it as "handling=False" so some other scheduler process can pick it up.
        self._execution_scheduling_timeout_threshold_ms = \
            cfg.CONF.scheduler.execution_scheduling_timeout_threshold_min * 60 * 1000
        self._coordinator = coordination_service.get_coordinator(start_heart=True)
        self._main_thread = None
        self._cleanup_thread = None 
Example #20
Source File: nfvo_plugin.py    From tacker with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(NfvoPlugin, self).__init__()
        self._pool = eventlet.GreenPool()
        self._vim_drivers = driver_manager.DriverManager(
            'tacker.nfvo.vim.drivers',
            cfg.CONF.nfvo_vim.vim_drivers)
        self.vim_client = vim_client.VimClient() 
Example #21
Source File: wsgi.py    From searchlight with Apache License 2.0 5 votes vote down vote up
def create_pool(self):
        return eventlet.GreenPool(size=self.threads) 
Example #22
Source File: st2-inject-trigger-instances.py    From st2 with Apache License 2.0 4 votes vote down vote up
def main():
    monkey_patch()

    cli_opts = [
        cfg.IntOpt('rate', default=100,
                   help='Rate of trigger injection measured in instances in per sec.' +
                   ' Assumes a default exponential distribution in time so arrival is poisson.'),
        cfg.ListOpt('triggers', required=False,
                    help='List of triggers for which instances should be fired.' +
                    ' Uniform distribution will be followed if there is more than one' +
                    'trigger.'),
        cfg.StrOpt('schema_file', default=None,
                   help='Path to schema file defining trigger and payload.'),
        cfg.IntOpt('duration', default=60,
                   help='Duration of stress test in seconds.'),
        cfg.BoolOpt('max-throughput', default=False,
                   help='If True, "rate" argument will be ignored and this script will try to '
                   'saturize the CPU and achieve max utilization.')
    ]
    do_register_cli_opts(cli_opts)
    config.parse_args()

    # Get config values
    triggers = cfg.CONF.triggers
    trigger_payload_schema = {}

    if not triggers:
        if (cfg.CONF.schema_file is None or cfg.CONF.schema_file == '' or
                not os.path.exists(cfg.CONF.schema_file)):
            print('Either "triggers" need to be provided or a schema file containing' +
                  ' triggers should be provided.')
            return
        with open(cfg.CONF.schema_file) as fd:
            trigger_payload_schema = yaml.safe_load(fd)
            triggers = list(trigger_payload_schema.keys())
            print('Triggers=%s' % triggers)

    rate = cfg.CONF.rate
    rate_per_trigger = int(rate / len(triggers))
    duration = cfg.CONF.duration
    max_throughput = cfg.CONF.max_throughput

    if max_throughput:
        rate = 0
        rate_per_trigger = 0

    dispatcher_pool = eventlet.GreenPool(len(triggers))

    for trigger in triggers:
        payload = trigger_payload_schema.get(trigger, {})
        dispatcher_pool.spawn(_inject_instances, trigger, rate_per_trigger, duration,
                              payload=payload, max_throughput=max_throughput)
        eventlet.sleep(random.uniform(0, 1))
    dispatcher_pool.waitall() 
Example #23
Source File: wsgi.py    From oslo.service with Apache License 2.0 4 votes vote down vote up
def __init__(self, conf, name, app, host='0.0.0.0', port=0,  # nosec
                 pool_size=None, protocol=eventlet.wsgi.HttpProtocol,
                 backlog=128, use_ssl=False, max_url_len=None,
                 logger_name='eventlet.wsgi.server',
                 socket_family=None, socket_file=None, socket_mode=None):
        """Initialize, but do not start, a WSGI server.

        :param conf: Instance of ConfigOpts.
        :param name: Pretty name for logging.
        :param app: The WSGI application to serve.
        :param host: IP address to serve the application.
        :param port: Port number to server the application.
        :param pool_size: Maximum number of eventlets to spawn concurrently.
        :param protocol: Protocol class.
        :param backlog: Maximum number of queued connections.
        :param use_ssl: Wraps the socket in an SSL context if True.
        :param max_url_len: Maximum length of permitted URLs.
        :param logger_name: The name for the logger.
        :param socket_family: Socket family.
        :param socket_file: location of UNIX socket.
        :param socket_mode: UNIX socket mode.
        :returns: None
        :raises: InvalidInput
        :raises: EnvironmentError
        """

        self.conf = conf
        self.conf.register_opts(_options.wsgi_opts)

        self.default_pool_size = self.conf.wsgi_default_pool_size

        # Allow operators to customize http requests max header line size.
        eventlet.wsgi.MAX_HEADER_LINE = conf.max_header_line
        self.name = name
        self.app = app
        self._server = None
        self._protocol = protocol
        self.pool_size = pool_size or self.default_pool_size
        self._pool = eventlet.GreenPool(self.pool_size)
        self._logger = logging.getLogger(logger_name)
        self._use_ssl = use_ssl
        self._max_url_len = max_url_len
        self.client_socket_timeout = conf.client_socket_timeout or None

        if backlog < 1:
            raise InvalidInput(reason=_('The backlog must be more than 0'))

        if not socket_family or socket_family in [socket.AF_INET,
                                                  socket.AF_INET6]:
            self.socket = self._get_socket(host, port, backlog)
        elif hasattr(socket, "AF_UNIX") and socket_family == socket.AF_UNIX:
            self.socket = self._get_unix_socket(socket_file, socket_mode,
                                                backlog)
        else:
            raise ValueError(_("Unsupported socket family: %s"), socket_family)

        (self.host, self.port) = self.socket.getsockname()[0:2]

        if self._use_ssl:
            sslutils.is_enabled(conf) 
Example #24
Source File: wsgi.py    From masakari with Apache License 2.0 4 votes vote down vote up
def __init__(self, name, app, host='0.0.0.0', port=0, pool_size=None,
                 protocol=eventlet.wsgi.HttpProtocol, backlog=128,
                 use_ssl=False, max_url_len=None):
        """Initialize, but do not start, a WSGI server.

        :param name: Pretty name for logging.
        :param app: The WSGI application to serve.
        :param host: IP address to serve the application.
        :param port: Port number to server the application.
        :param pool_size: Maximum number of eventlets to spawn concurrently.
        :param backlog: Maximum number of queued connections.
        :param max_url_len: Maximum length of permitted URLs.
        :returns: None
        :raises: masakari.exception.InvalidInput
        """
        # Allow operators to customize http requests max header line size.
        eventlet.wsgi.MAX_HEADER_LINE = CONF.wsgi.max_header_line
        self.name = name
        self.app = app
        self._server = None
        self._protocol = protocol
        self.pool_size = pool_size or self.default_pool_size
        self._pool = eventlet.GreenPool(self.pool_size)
        self._logger = logging.getLogger("masakari.%s.wsgi.server" % self.name)
        self._use_ssl = use_ssl
        self._max_url_len = max_url_len

        self.client_socket_timeout = CONF.wsgi.client_socket_timeout or None

        if backlog < 1:
            raise exception.InvalidInput(
                reason=_('The backlog must be more than 0'))

        bind_addr = (host, port)
        try:
            info = socket.getaddrinfo(bind_addr[0],
                                      bind_addr[1],
                                      socket.AF_UNSPEC,
                                      socket.SOCK_STREAM)[0]
            family = info[0]
            bind_addr = info[-1]
        except Exception:
            family = socket.AF_INET

        try:
            self._socket = eventlet.listen(bind_addr, family, backlog=backlog)
        except EnvironmentError:
            LOG.error("Could not bind to %(host)s:%(port)d",
                      {'host': host, 'port': port})
            raise

        (self.host, self.port) = self._socket.getsockname()[0:2]
        LOG.info("%(name)s listening on %(host)s:%(port)d",
                 {'name': self.name, 'host': self.host, 'port': self.port})