Python eventlet.Timeout() Examples

The following are 25 code examples of eventlet.Timeout(). 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_obj.py    From oio-swift with Apache License 2.0 6 votes vote down vote up
def test_PUT_timeout_during_data_transfer(self):
        """The gateway times out while upload data to the server."""
        class FakeReader(object):
            def read(self, size):
                raise Timeout()

        req = Request.blank('/v1/a/c/o.jpg', method='PUT',
                            body='test body')
        req.environ['wsgi.input'] = FakeReader()
        req.headers['content-length'] = '6'
        chunks = [{"url": "http://127.0.0.1:7000/AAAA", "pos": "0", "size": 6}]
        meta = fake_prepare_meta()
        self.storage.container.content_prepare = Mock(
            return_value=(meta, chunks))
        with patch('oio.api.replication.io.http_connect',
                   new=fake_http_connect):
            resp = req.get_response(self.app)
            self.assertEqual(503, resp.status_int) 
Example #2
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 #3
Source File: test_dnsutils.py    From designate with Apache License 2.0 6 votes vote down vote up
def test_do_afxr_fails_with_timeout(self, mock_cancel, mock_from_xfr,
                                        mock_xfr):
        mock_from_xfr.side_effect = eventlet.Timeout()

        masters = [
            {'host': '192.168.0.1', 'port': 53},
            {'host': '192.168.0.2', 'port': 53},
            {'host': '192.168.0.3', 'port': 53},
            {'host': '192.168.0.4', 'port': 53},
        ]

        self.assertRaises(
            exceptions.XFRFailure,
            dnsutils.do_axfr, 'example.com.', masters,
        )

        self.assertTrue(mock_xfr.called)
        self.assertTrue(mock_from_xfr.called)
        self.assertTrue(mock_cancel.called) 
Example #4
Source File: impl_dynect.py    From designate with Apache License 2.0 6 votes vote down vote up
def poll_response(self, response):
        """
        The API might return a job nr in the response in case of a async
        response: https://github.com/fog/fog/issues/575
        """
        status = response.status

        timeout = Timeout(CONF[CFG_GROUP_NAME].job_timeout)
        try:
            while status == 307:
                time.sleep(1)
                url = response.headers.get('Location')
                LOG.debug("Polling %s", url)

                polled_response = self.get(url)
                status = response.status
        except Timeout as t:
            if t == timeout:
                raise DynTimeoutError('Timeout reached when pulling job.')
        finally:
            timeout.cancel()
        return polled_response 
Example #5
Source File: custom_evenlet_pool_executor.py    From distributed_framework with Apache License 2.0 6 votes vote down vote up
def evenlet_timeout_deco(timeout_t):
    def _evenlet_timeout_deco(f):
        def __evenlet_timeout_deco(*args, **kwargs):
            timeout = Timeout(timeout_t, )
            # timeout.start()  # 与gevent不一样,直接start了。
            result = None
            try:
                result = f(*args, **kwargs)
            except Timeout as t:
                logger_evenlet_timeout_deco.error(f'函数 {f} 运行超过了 {timeout_t} 秒')
                if t is not timeout:
                    nb_print(t)
                    # raise  # not my timeout
            finally:
                timeout.cancel()
                return result

        return __evenlet_timeout_deco

    return _evenlet_timeout_deco 
Example #6
Source File: base.py    From cyborg with Apache License 2.0 6 votes vote down vote up
def get_path(self, project_file=None):
        """Get the absolute path to a file. Used for testing the API.

        :param project_file: File whose path to return. Default: None.
        :returns: path to the specified file, or path to project root.
        """
        root = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..')
            )
        if project_file:
            return os.path.join(root, project_file)
        else:
            return root


# Test worker cannot survive eventlet's Timeout exception, which effectively
# kills the whole worker, with all test cases scheduled to it. This metaclass
# makes all test cases convert Timeout exceptions into unittest friendly
# failure mode (self.fail). 
Example #7
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 #8
Source File: app_testing_objects.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(TimeoutException, self).__init__('Timeout') 
Example #9
Source File: __main__.py    From CG-Accumulator with MIT License 5 votes vote down vote up
def connect(fname, url_to_scrape):
    """Connect to the given url, Return HTML content

    Use the text file with name fname for the parsed HTML
    Return the parsed HTML content as a string
    Return 'Exit' if connection failure occurs 3 times
    """
    try:
        with eventlet.Timeout(12, Exception):
            r = requests.get(url_to_scrape, verify = False)
        soup = BeautifulSoup(r.text, "html.parser")
        with open(fname, "w") as text_file:
            text_file.write("{}".format(soup))
        with open(fname) as f:
            content = f.readlines()
        connect.counter = 0
        return content
    #except requests.exceptions.RequestException:
    except Exception:
        # Static var storing then number of times attempt to connect has failed
        # If >=4, then we assume that user is not connected to the internet.       
	connect.counter += 1
        if connect.counter >= 4:
            connect.counter = 0
            print '\a'
            print '\nYou don\'t seem to be having internet connectivity | Connection with ERP performance page cannot be established.'
            reconnect_choice = raw_input('Enter r to try again, x to exit :  ')
            while reconnect_choice not in ['r', 'x']:
                reconnect_choice = raw_input('Invalid choice! Please enter r to try reconnecting again, or enter x to exit :  ')
            if reconnect_choice == 'r':
                print 'Retrying....'
                return connect(fname, url_to_scrape)
            else:
                print "\nExiting...."
                exit(0)
        else:
            if connect.counter == 1:
                print 'Experiencing slow internet connectivity...'
            # print 'Connection Error'
            # print 'Retrying....'
            return connect(fname, url_to_scrape) 
Example #10
Source File: storlet_handler.py    From storlets with Apache License 2.0 5 votes vote down vote up
def __call__(self, req):
        try:
            request_handler = self.handler_class(
                req, self.conf, self.gateway_conf, self.app, self.logger)
            self.logger.debug('storlet_handler call in %s: with %s/%s/%s' %
                              (self.exec_server, request_handler.account,
                               request_handler.container, request_handler.obj))
        except HTTPException:
            raise
        except NotStorletRequest:
            return req.get_response(self.app)

        try:
            return request_handler.handle_request()

        # TODO(takashi): Consider handling them in lower layers
        except StorletTimeout:
            self.logger.exception('Storlet execution timed out')
            raise HTTPInternalServerError(body='Storlet execution timed out')
        except StorletRuntimeException:
            self.logger.exception('Storlet execution failed')
            raise HTTPInternalServerError(body='Storlet execution failed')
        except Timeout:
            self.logger.exception('Internal request timed out')
            raise HTTPInternalServerError(body='Internal request timed out')
        except HTTPException:
            # TODO(takashi): Shoud we generate this log for all error?
            #                (ex. 404 when the object is not found)
            self.logger.exception('Storlet execution failed')
            raise
        except Exception:
            self.logger.exception('Internal server error')
            raise HTTPInternalServerError(body='Internal server error') 
Example #11
Source File: utils.py    From karbor with Apache License 2.0 5 votes vote down vote up
def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
    """Wait until callable predicate is evaluated as True

    :param predicate: Callable deciding whether waiting should continue.
     Best practice is to instantiate predicate with functools.partial()
    :param timeout: Timeout in seconds how long should function wait.
    :param sleep: Polling interval for results in seconds.
    :param exception: Exception class for eventlet.Timeout.
     (see doc for eventlet.Timeout for more information)

    """
    with eventlet.timeout.Timeout(timeout, exception):
        while not predicate():
            eventlet.sleep(sleep) 
Example #12
Source File: base.py    From cyborg with Apache License 2.0 5 votes vote down vote up
def assert_max_execution_time(self, max_execution_time=5):
        with eventlet.Timeout(max_execution_time, False):
            yield
            return
        self.fail('Execution of this test timed out') 
Example #13
Source File: conftest.py    From detox with MIT License 5 votes vote down vote up
def test_hang(testdir):
    p = py.path.local(__file__).dirpath("conftest.py")
    p.copy(testdir.tmpdir.join(p.basename))
    testdir.makepyfile(
        """
        import pytest
        from eventlet.green import time
        @pytest.mark.timeout(0.01)
        def test_hang():
            time.sleep(3.0)
    """
    )
    result = testdir.runpytest()
    assert "failed to timeout" not in result.stdout.str()
    result.stdout.fnmatch_lines(["*Timeout: 0.01*"]) 
Example #14
Source File: conftest.py    From detox with MIT License 5 votes vote down vote up
def with_timeout(request):
    marker = request.node.get_closest_marker("timeout")
    timeout = marker.args[0] if marker else 5.0
    with eventlet.Timeout(timeout):
        yield 
Example #15
Source File: conftest.py    From detox with MIT License 5 votes vote down vote up
def pytest_configure(config):
    config.addinivalue_line("markers", "example1: use example1 for setup")
    config.addinivalue_line("markers", "example2: use example2 for setup")
    config.addinivalue_line(
        "markers",
        "timeout(N): stop test function " "after N seconds, throwing a Timeout.",
    ) 
Example #16
Source File: scheduling.py    From watcher with Apache License 2.0 5 votes vote down vote up
def _as_timed_sync_func(self, sync_func, name, timeout):
        def _timed_sync():
            with eventlet.Timeout(
                timeout,
                exception=exception.ClusterDataModelCollectionError(cdm=name)
            ):
                sync_func()

        return _timed_sync 
Example #17
Source File: start.py    From geth-hack with MIT License 5 votes vote down vote up
def tara(ip):
	try:
		with eventlet.Timeout(3):
			address = 'http://'+ip+':8545'
			data = {"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}
			headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
			r = requests.post(address, data=json.dumps(data), headers=headers, timeout=3)
			return r.text
	except: 
	  pass 
Example #18
Source File: app_testing_objects.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def wait(self, timeout, exception):
        with eventlet.Timeout(timeout, exception):
            try:
                self.daemon.wait()
            except greenlet.GreenletExit:
                return True 
Example #19
Source File: geventlet.py    From Flask-P2P with MIT License 5 votes vote down vote up
def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive or None, False) 
Example #20
Source File: eventlet_.py    From wampy with Mozilla Public License 2.0 5 votes vote down vote up
def _wait_for_message(self, timeout):
        q = self.message_queue

        with eventlet.Timeout(timeout):
            while q.qsize() == 0:
                eventlet.sleep()

        message = q.get()
        return message 
Example #21
Source File: eventlet_.py    From wampy with Mozilla Public License 2.0 5 votes vote down vote up
def receive_message(self, timeout):
        try:
            message = self._wait_for_message(timeout)
        except eventlet.Timeout:
            raise WampyTimeOutError(
                "no message returned (timed-out in {})".format(timeout)
            )
        return message 
Example #22
Source File: eventlet_.py    From wampy with Mozilla Public License 2.0 5 votes vote down vote up
def Timeout(self, timeout, raise_after=True):
        return eventlet.Timeout(timeout, raise_after) 
Example #23
Source File: geventlet.py    From jbox with MIT License 5 votes vote down vote up
def timeout_ctx(self):
        return eventlet.Timeout(self.cfg.keepalive or None, False) 
Example #24
Source File: driver.py    From zun with Apache License 2.0 4 votes vote down vote up
def execute_run(self, exec_id, command):
        with docker_utils.docker_client() as docker:
            try:
                with eventlet.Timeout(CONF.docker.execute_timeout):
                    output = docker.exec_start(exec_id, False, False, False)
            except eventlet.Timeout:
                raise exception.Conflict(_(
                    "Timeout on executing command: %s") % command)
            inspect_res = docker.exec_inspect(exec_id)
            return output, inspect_res['ExitCode'] 
Example #25
Source File: dnsutils.py    From designate with Apache License 2.0 4 votes vote down vote up
def do_axfr(zone_name, servers, timeout=None, source=None):
    """
    Requests an AXFR for a given zone name and process the response

    :returns: Zone instance from dnspython
    """
    random.shuffle(servers)
    timeout = timeout or CONF["service:mdns"].xfr_timeout

    xfr = None

    for srv in servers:
        to = eventlet.Timeout(timeout)
        log_info = {'name': zone_name, 'host': srv}
        try:
            LOG.info("Doing AXFR for %(name)s from %(host)s", log_info)

            xfr = dns.query.xfr(srv['host'], zone_name, relativize=False,
                                timeout=1, port=srv['port'], source=source)
            raw_zone = dns.zone.from_xfr(xfr, relativize=False)
            break
        except eventlet.Timeout as t:
            if t == to:
                LOG.error("AXFR timed out for %(name)s from %(host)s",
                          log_info)
                continue
        except dns.exception.FormError:
            LOG.error("Zone %(name)s is not present on %(host)s."
                      "Trying next server.", log_info)
        except socket.error:
            LOG.error("Connection error when doing AXFR for %(name)s from "
                      "%(host)s", log_info)
        except Exception:
            LOG.exception("Problem doing AXFR %(name)s from %(host)s. "
                          "Trying next server.", log_info)
        finally:
            to.cancel()
        continue
    else:
        raise exceptions.XFRFailure(
            "XFR failed for %(name)s. No servers in %(servers)s was reached." %
            {"name": zone_name, "servers": servers})

    LOG.debug("AXFR Successful for %s", raw_zone.origin.to_text())

    return raw_zone