Python eventlet.monkey_patch() Examples

The following are 30 code examples of eventlet.monkey_patch(). 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: conftest.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def maybe_monkeypatched_threading(request):
    if request.param == "eventlet":
        try:
            eventlet.monkey_patch()
        except AttributeError as e:
            if "'thread.RLock' object has no attribute" in str(e):
                # https://bitbucket.org/pypy/pypy/issues/2962/gevent-cannot-patch-rlock-under-pypy-27-7
                pytest.skip("https://github.com/eventlet/eventlet/issues/546")
            else:
                raise
    elif request.param == "gevent":
        try:
            gevent.monkey.patch_all()
        except Exception as e:
            if "_RLock__owner" in str(e):
                pytest.skip("https://github.com/gevent/gevent/issues/1380")
            else:
                raise
    else:
        assert request.param is None

    return request.param 
Example #2
Source File: bottle.py    From EasY_HaCk with Apache License 2.0 6 votes vote down vote up
def run(self, handler):
        from eventlet import wsgi, listen, patcher
        if not patcher.is_monkey_patched(os):
            msg = "Bottle requires eventlet.monkey_patch() (before import)"
            raise RuntimeError(msg)
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            wsgi.server(listen(address, **socket_args), handler,
                        log_output=(not self.quiet))
        except TypeError:
            # Fallback, if we have old version of eventlet
            wsgi.server(listen(address), handler) 
Example #3
Source File: anyserver.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(servername, ip, port, softcron=True, logging=False, profiler=None,
        options=None):
    if servername == 'gevent':
        from gevent import monkey
        monkey.patch_all()
    elif servername == 'eventlet':
        import eventlet
        eventlet.monkey_patch()

    import gluon.main

    if logging:
        application = gluon.main.appfactory(wsgiapp=gluon.main.wsgibase,
                                            logfilename='httpserver.log',
                                            profiler_dir=profiler)
    else:
        application = gluon.main.wsgibase
    if softcron:
        from gluon.settings import global_settings
        global_settings.web2py_crontype = 'soft'
    getattr(Servers, servername)(application, (ip, int(port)), options=options) 
Example #4
Source File: bottle.py    From POC-EXP with GNU General Public License v3.0 6 votes vote down vote up
def run(self, handler):
        from eventlet import wsgi, listen, patcher
        if not patcher.is_monkey_patched(os):
            msg = "Bottle requires eventlet.monkey_patch() (before import)"
            raise RuntimeError(msg)
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            wsgi.server(listen(address, **socket_args), handler,
                        log_output=(not self.quiet))
        except TypeError:
            # Fallback, if we have old version of eventlet
            wsgi.server(listen(address), handler) 
Example #5
Source File: bottle.py    From slack-machine with MIT License 6 votes vote down vote up
def run(self, handler):
        from eventlet import wsgi, listen, patcher
        if not patcher.is_monkey_patched(os):
            msg = "Bottle requires eventlet.monkey_patch() (before import)"
            raise RuntimeError(msg)
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            wsgi.server(listen(address, **socket_args), handler,
                        log_output=(not self.quiet))
        except TypeError:
            # Fallback, if we have old version of eventlet
            wsgi.server(listen(address), handler) 
Example #6
Source File: monkey_patch.py    From st2 with Apache License 2.0 6 votes vote down vote up
def monkey_patch(patch_thread=None):
    """
    Function which performs eventlet monkey patching and also takes into account "--use-debugger"
    argument in the command line arguments.

    If this argument is found, no monkey patching is performed for the thread module. This allows
    user to use remote debuggers.

    :param patch_thread: True to also patch the thread module. If not provided, thread module is
                         patched unless debugger is used.
    :type patch_thread: ``bool``
    """
    import eventlet

    if patch_thread is None:
        patch_thread = not is_use_debugger_flag_provided()

    eventlet.monkey_patch(os=True, select=True, socket=True, thread=patch_thread, time=True) 
Example #7
Source File: test_wsgi.py    From oslo.service with Apache License 2.0 6 votes vote down vote up
def test_uri_length_limit(self):
        eventlet.monkey_patch(os=False, thread=False)
        server = wsgi.Server(self.conf, "test_uri_length_limit", None,
                             host="127.0.0.1", max_url_len=16384, port=33337)
        server.start()
        self.assertFalse(server._server.dead)

        uri = "http://127.0.0.1:%d/%s" % (server.port, 10000 * 'x')
        resp = requests.get(uri, proxies={"http": ""})
        eventlet.sleep(0)
        self.assertNotEqual(requests.codes.REQUEST_URI_TOO_LARGE,
                            resp.status_code)

        uri = "http://127.0.0.1:%d/%s" % (server.port, 20000 * 'x')
        resp = requests.get(uri, proxies={"http": ""})
        eventlet.sleep(0)
        self.assertEqual(requests.codes.REQUEST_URI_TOO_LARGE,
                         resp.status_code)
        server.stop()
        server.wait() 
Example #8
Source File: eventlet_backdoor.py    From oslo.service with Apache License 2.0 6 votes vote down vote up
def _main():
    import eventlet
    eventlet.monkey_patch(all=True)
    # Monkey patch the original current_thread to use the up-to-date _active
    # global variable. See https://bugs.launchpad.net/bugs/1863021 and
    # https://github.com/eventlet/eventlet/issues/592
    import __original_module_threading as orig_threading
    import threading  # noqa
    orig_threading.current_thread.__globals__['_active'] = threading._active

    from oslo_config import cfg

    logging.basicConfig(level=logging.DEBUG)

    conf = cfg.ConfigOpts()
    conf.register_cli_opts(_options.eventlet_backdoor_opts)
    conf(sys.argv[1:])

    where_running_thread = _initialize_if_enabled(conf)
    if not where_running_thread:
        raise RuntimeError(_("Did not create backdoor at requested location"))
    else:
        _where_running, thread = where_running_thread
        thread.wait() 
Example #9
Source File: bottle.py    From warriorframework with Apache License 2.0 6 votes vote down vote up
def run(self, handler):
        from eventlet import wsgi, listen, patcher
        if not patcher.is_monkey_patched(os):
            msg = "Bottle requires eventlet.monkey_patch() (before import)"
            raise RuntimeError(msg)
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            wsgi.server(listen(address, **socket_args), handler,
                        log_output=(not self.quiet))
        except TypeError:
            # Fallback, if we have old version of eventlet
            wsgi.server(listen(address), handler) 
Example #10
Source File: test_connection_pool.py    From oslo.cache with Apache License 2.0 6 votes vote down vote up
def test_can_create_with_kwargs(self):
        """Test for lp 1812935

        Note that in order to reproduce the bug, it is necessary to add the
        following to the top of oslo_cache/tests/__init__.py::

            import eventlet
            eventlet.monkey_patch()

        This should happen before any other imports in that file.
        """
        client = _memcache_pool._MemcacheClient('foo', check_keys=False)
        # Make sure kwargs are properly processed by the client
        self.assertFalse(client.do_check_key)
        # Make sure our __new__ override still results in the right type
        self.assertIsInstance(client, _memcache_pool._MemcacheClient) 
Example #11
Source File: share.py    From manila with Apache License 2.0 6 votes vote down vote up
def main():
    log.register_options(CONF)
    gmr_opts.set_defaults(CONF)
    CONF(sys.argv[1:], project='manila',
         version=version.version_string())
    log.setup(CONF, "manila")
    utils.monkey_patch()
    gmr.TextGuruMeditation.setup_autorun(version, conf=CONF)
    launcher = service.process_launcher()
    if CONF.enabled_share_backends:
        for backend in CONF.enabled_share_backends:
            host = "%s@%s" % (CONF.host, backend)
            server = service.Service.create(host=host,
                                            service_name=backend,
                                            binary='manila-share',
                                            coordination=True)
            launcher.launch_service(server)
    else:
        server = service.Service.create(binary='manila-share')
        launcher.launch_service(server)
    launcher.wait() 
Example #12
Source File: manage.py    From git-webhook with MIT License 6 votes vote down vote up
def __call__(self, app, host, port, use_debugger, use_reloader):
        # override the default runserver command to start a Socket.IO server
        if use_debugger is None:
            use_debugger = app.debug
            if use_debugger is None:
                use_debugger = True
        if use_reloader is None:
            use_reloader = app.debug
        import eventlet
        # monkey_patch
        eventlet.monkey_patch()

        socketio.run(app,
                     host=host,
                     port=port,
                     debug=use_debugger,
                     use_reloader=use_reloader,
                     **self.server_options) 
Example #13
Source File: bottle.py    From silvia-pi with MIT License 6 votes vote down vote up
def run(self, handler):
        from eventlet import wsgi, listen, patcher
        if not patcher.is_monkey_patched(os):
            msg = "Bottle requires eventlet.monkey_patch() (before import)"
            raise RuntimeError(msg)
        socket_args = {}
        for arg in ('backlog', 'family'):
            try:
                socket_args[arg] = self.options.pop(arg)
            except KeyError:
                pass
        address = (self.host, self.port)
        try:
            wsgi.server(listen(address, **socket_args), handler,
                        log_output=(not self.quiet))
        except TypeError:
            # Fallback, if we have old version of eventlet
            wsgi.server(listen(address), handler) 
Example #14
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 #15
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 #16
Source File: api.py    From manila with Apache License 2.0 5 votes vote down vote up
def main():
    log.register_options(CONF)
    gmr_opts.set_defaults(CONF)
    CONF(sys.argv[1:], project='manila',
         version=version.version_string())
    config.verify_share_protocols()
    log.setup(CONF, "manila")
    utils.monkey_patch()

    gmr.TextGuruMeditation.setup_autorun(version, conf=CONF)
    launcher = service.process_launcher()
    server = service.WSGIService('osapi_share')
    launcher.launch_service(server, workers=server.workers or 1)
    launcher.wait() 
Example #17
Source File: geventlet.py    From jbox with MIT License 5 votes vote down vote up
def patch(self):
        hubs.use_hub()
        eventlet.monkey_patch(os=False)
        patch_sendfile() 
Example #18
Source File: bottle.py    From EasY_HaCk with Apache License 2.0 5 votes vote down vote up
def _cli_patch(args):
    opts, _, _ = _cli_parse(args)
    if opts.server:
        if opts.server.startswith('gevent'):
            import gevent.monkey
            gevent.monkey.patch_all()
        elif opts.server.startswith('eventlet'):
            import eventlet
            eventlet.monkey_patch() 
Example #19
Source File: bottle.py    From silvia-pi with MIT License 5 votes vote down vote up
def _cli_patch(cli_args):  # pragma: no coverage
    parsed_args, _ = _cli_parse(cli_args)
    opts = parsed_args
    if opts.server:
        if opts.server.startswith('gevent'):
            import gevent.monkey
            gevent.monkey.patch_all()
        elif opts.server.startswith('eventlet'):
            import eventlet
            eventlet.monkey_patch() 
Example #20
Source File: main.py    From mochi with MIT License 5 votes vote down vote up
def init(no_monkeypatch=False):
    if hasattr(init, '__called') and init.__called:
        return
    else:
        init.__called = True

    import eventlet

    def eval_from_file(path_obj):
        """Evaluate sexpression in given file.
        """
        with path_obj.open() as fobj:
            expr = fobj.read()
        eval_sexp_str(expr)

    if no_monkeypatch:
        pass
    elif (not GE_PYTHON_33) or platform().lower().startswith('win'):
        eventlet.monkey_patch(os=False)
    else:
        eventlet.monkey_patch()
    expr_path = Path(__file__).absolute().parents[1] / 'sexpressions'
    eval_from_file(expr_path / 'main.expr')
    if not IS_PYPY:
        eval_from_file(expr_path / 'cpython.expr')
    else:
        eval_from_file(expr_path / 'pypy.expr')

    eval_from_file(expr_path / 'del_hidden.expr')

    for syntax in {'for', 'each', 'while', 'break', 'continue'}:
        del syntax_table[syntax]
        del global_env[syntax]
        del global_scope[syntax]

    sys.path.append(os.getcwd())
    from mochi.utils.importer import set_importer
    set_importer() 
Example #21
Source File: messaging.py    From senlin with Apache License 2.0 5 votes vote down vote up
def setup(url=None, optional=False):
    """Initialise the oslo_messaging layer."""
    global TRANSPORT, GLOBAL_TRANSPORT, NOTIFIER

    if url and url.startswith("fake://"):
        # NOTE: oslo_messaging fake driver uses time.sleep
        # for task switch, so we need to monkey_patch it
        eventlet.monkey_patch(time=True)

    messaging.set_transport_defaults('senlin')
    if not TRANSPORT:
        exmods = ['senlin.common.exception']
        try:
            TRANSPORT = messaging.get_rpc_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods)
        except messaging.InvalidTransportURL as e:
            TRANSPORT = None
            if not optional or e.url:
                # NOTE: oslo_messaging is configured but unloadable
                # so reraise the exception
                raise

    if not NOTIFIER:
        exmods = ['senlin.common.exception']
        try:
            NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
                cfg.CONF, allowed_remote_exmods=exmods)
        except Exception:
            raise

        serializer = RequestContextSerializer(JsonPayloadSerializer())
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer,
                                      topics=cfg.CONF.notification_topics) 
Example #22
Source File: bottle.py    From POC-EXP with GNU General Public License v3.0 5 votes vote down vote up
def _cli_patch(args):
    opts, _, _ = _cli_parse(args)
    if opts.server:
        if opts.server.startswith('gevent'):
            import gevent.monkey
            gevent.monkey.patch_all()
        elif opts.server.startswith('eventlet'):
            import eventlet
            eventlet.monkey_patch() 
Example #23
Source File: executor.py    From forge with Apache License 2.0 5 votes vote down vote up
def setup(cls):
        eventlet.sleep() # workaround for import cycle: https://github.com/eventlet/eventlet/issues/401
        eventlet.monkey_patch(thread=False)

        if 'pytest' not in sys.modules:
            import getpass
            getpass.os = eventlet.patcher.original('os') # workaround for https://github.com/eventlet/eventlet/issues/340

        sys.stdout = cls.MUXER 
Example #24
Source File: bottle.py    From slack-machine with MIT License 5 votes vote down vote up
def _cli_patch(cli_args):  # pragma: no coverage
    parsed_args, _ = _cli_parse(cli_args)
    opts = parsed_args
    if opts.server:
        if opts.server.startswith('gevent'):
            import gevent.monkey
            gevent.monkey.patch_all()
        elif opts.server.startswith('eventlet'):
            import eventlet
            eventlet.monkey_patch() 
Example #25
Source File: base.py    From st2 with Apache License 2.0 5 votes vote down vote up
def tearDownClass(cls):
        eventlet.monkey_patch(
            os=False,
            select=False,
            socket=False,
            thread=False,
            time=False
        ) 
Example #26
Source File: base.py    From st2 with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        eventlet.monkey_patch(
            os=True,
            select=True,
            socket=True,
            thread=False if '--use-debugger' in sys.argv else True,
            time=True
        ) 
Example #27
Source File: eventlet_utils.py    From tacker with Apache License 2.0 5 votes vote down vote up
def monkey_patch():
    eventlet.monkey_patch()
    if os.name != 'nt':
        p_c_e = importutils.import_module('pyroute2.config.asyncio')
        p_c_e.asyncio_config() 
Example #28
Source File: test_wsgi.py    From oslo.service with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(TestWSGIServerWithSSL, self).setUp()
        cert_file_name = os.path.join(SSL_CERT_DIR, 'certificate.crt')
        key_file_name = os.path.join(SSL_CERT_DIR, 'privatekey.key')
        eventlet.monkey_patch(os=False, thread=False)
        self.host = "127.0.0.1"

        self.config(cert_file=cert_file_name,
                    key_file=key_file_name,
                    group=sslutils.config_section) 
Example #29
Source File: scheduler.py    From manila with Apache License 2.0 5 votes vote down vote up
def main():
    log.register_options(CONF)
    gmr_opts.set_defaults(CONF)
    CONF(sys.argv[1:], project='manila',
         version=version.version_string())
    log.setup(CONF, "manila")
    utils.monkey_patch()
    gmr.TextGuruMeditation.setup_autorun(version, conf=CONF)
    server = service.Service.create(binary='manila-scheduler',
                                    coordination=True)
    service.serve(server)
    service.wait() 
Example #30
Source File: bottle.py    From warriorframework with Apache License 2.0 5 votes vote down vote up
def _cli_patch(args):
    opts, _, _ = _cli_parse(args)
    if opts.server:
        if opts.server.startswith('gevent'):
            import gevent.monkey
            gevent.monkey.patch_all()
        elif opts.server.startswith('eventlet'):
            import eventlet
            eventlet.monkey_patch()