python类Timeout()的实例源码

Peer.py 文件源码 项目:zeronet-debian 作者: bashrc 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def ping(self):
        response_time = None
        for retry in range(1, 3):  # Retry 3 times
            s = time.time()
            with gevent.Timeout(10.0, False):  # 10 sec timeout, don't raise exception
                res = self.request("ping")

                if res and "body" in res and res["body"] == "Pong!":
                    response_time = time.time() - s
                    break  # All fine, exit from for loop
            # Timeout reached or bad response
            self.onConnectionError()
            self.connect()
            time.sleep(1)

        if response_time:
            self.log("Ping: %.3f" % response_time)
        else:
            self.log("Ping failed")
        self.last_ping = response_time
        return response_time

    # Request peer exchange from peer
TestConnectionServer.py 文件源码 项目:zeronet-debian 作者: bashrc 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def testFloodProtection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        whitelist = file_server.whitelist  # Save for reset
        file_server.whitelist = []  # Disable 127.0.0.1 whitelist
        client = ConnectionServer("127.0.0.1", 1545)

        # Only allow 6 connection in 1 minute
        for reconnect in range(6):
            connection = client.getConnection("127.0.0.1", 1544)
            assert connection.handshake
            connection.close()

        # The 7. one will timeout
        with pytest.raises(gevent.Timeout):
            with gevent.Timeout(0.1):
                connection = client.getConnection("127.0.0.1", 1544)

        # Reset whitelist
        file_server.whitelist = whitelist
SiteManagerPlugin.py 文件源码 项目:zeronet-debian 作者: bashrc 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def resolveDomainDnschainNet(self, domain):
        try:
            match = self.isDomain(domain)
            sub_domain = match.group(1).strip(".")
            top_domain = match.group(2)
            if not sub_domain: sub_domain = "@"
            address = None
            with gevent.Timeout(5, Exception("Timeout: 5s")):
                res = Http.get("https://api.dnschain.net/v1/namecoin/key/%s" % top_domain).read()
                data = json.loads(res)["data"]["value"]
                if "zeronet" in data:
                    for key, val in data["zeronet"].iteritems():
                        self.dns_cache[key+"."+top_domain] = [val, time.time()+60*60*5] # Cache for 5 hours
                    self.saveDnsCache()
                    return data["zeronet"].get(sub_domain)
            # Not found
            return address
        except Exception, err:
            log.debug("Dnschain.net %s resolve error: %s" % (domain, Debug.formatException(err)))


    # Resolve domain using dnschain.info
    # Return: The address or None
test_gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_whatever_1(self):
        """
        From a writing child, fire into the pipe. In a greenlet in the parent,
        receive one of these messages and return it to the main greenlet.
        Expect message retrieval (child process creation) within a certain
        timeout interval. Terminate the child process after retrieval.
        """
        with pipe() as (r, w):
            def readgreenlet(reader):
                with gevent.Timeout(SHORTTIME * 5, False) as t:
                    m = reader.get(timeout=t)
                    return m
            p = start_process(usecase_child_a, args=(w, ))
            # Wait for process to send first message:
            r.get()
            # Second message must be available immediately now.
            g = gevent.spawn(readgreenlet, r)
            m = r.get()
            assert g.get() == "SPLASH"
            p.terminate()
            p.join()
            assert p.exitcode == -signal.SIGTERM
__init__.py 文件源码 项目:kingpin 作者: pinterest 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _stop_client(self):
        """Best effort to stop the client."""
        try:
            # Make sure not to mistake this scenario with failing to stop
            # client.
            if self._client is None:
                log.info("Kazoo client is None.")
                return

            _retry((Exception,), tries=3, delay=1, backoff=2,
                   sleep_func=gevent.sleep)(self._client.stop)()

            log.info("Successfully stopped kazoo client.")
        except (Exception, gevent.Timeout):
            self._sc.increment("errors.zk.client.stop.failure",
                               tags={'host': hostname},
                               sample_rate=1)
            log.exception("Failed to stop kazoo client.")
client.py 文件源码 项目:raiden 作者: raiden-network 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def check_node_connection(func):
    """ A decorator to reconnect if the connection to the node is lost."""
    def retry_on_disconnect(self, *args, **kwargs):
        for i, timeout in enumerate(timeout_two_stage(10, 3, 10)):
            try:
                result = func(self, *args, **kwargs)
                if i > 0:
                    log.info('Client reconnected')
                return result

            except (requests.exceptions.ConnectionError, InvalidReplyError):
                log.info(
                    'Timeout in eth client connection to {}. Is the client offline? Trying '
                    'again in {}s.'.format(self.transport.endpoint, timeout)
                )
            gevent.sleep(timeout)

    return retry_on_disconnect
octp_server.py 文件源码 项目:octopus 作者: ideascf 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _publish(self):
        """
        Start coroutine for publish.
        :return:
        """

        for retry in range(constant.ETCD_RECONNECT_MAX_RETRY_INIT):
            try:
                co = gevent.spawn(self._publish_handler)
                co.join(constant.ETCD_CONNECT_TIMEOUT)

                e = co.exception
                if e:  # if _publish_handler raise some exception, reraise it.
                    raise e
                else:
                    co.kill()
            except (etcd.EtcdConnectionFailed, gevent.Timeout):
                log.info('Connect to etcd failed, Retry(%d)...', retry)
                gevent.sleep(constant.ETCD_RECONNECT_INTERVAL)
            else:
                log.info('Publish OK.')
                break
        else:  # publish failed
            raise err.OctpEtcdConnectError('Max attempts exceeded.')
3_workersjobs.py 文件源码 项目:jumpscale_portal 作者: jumpscale7 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def main(j, args, params, tags, tasklet):

    import JumpScale.grid.agentcontroller
    import gevent

    doc = args.doc
    params.result = (doc, doc)
    nid = args.getTag('nid')

    node_exists = j.core.portal.active.osis.exists('system', 'node', int(nid))
    if node_exists:
        node = j.core.portal.active.osis.get('system', 'node', int(nid))
        try:
            workerscl = j.clients.agentcontroller.getProxy(category="worker")
            with gevent.Timeout(5):
                jobs = workerscl.getQueuedJobs(queue=None, format='json', _agentid=nid)
            doc.applyTemplate({'name': node['name'], 'jobs': jobs})
        except gevent.Timeout:
            doc.applyTemplate({'name': node['name']})
    else:
        doc.applyTemplate({})
    return params
server.py 文件源码 项目:katana-sdk-python2 作者: kusanagi 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __process_request(self, stream, pid, timeout):
        # Process request and get response stream.
        # Request are processed inside a thread pool to avoid
        # userland code to block requests.
        res = self._pool.spawn(self.__process_request_stream, stream)

        # Wait for a period of seconds to get the execution result
        try:
            response = res.get(timeout=timeout)
        except gevent.Timeout:
            msg = 'SDK execution timed out after {}ms'.format(
                int(timeout * 1000),
                pid,
                )
            response = create_error_response(msg)
            LOG.warn('{}. PID: {}'.format(msg, pid))
        except:
            LOG.exception('Failed to handle request. PID: %d', pid)
            response = create_error_response('Failed to handle request')

        self._send_response(response)
test_runners.py 文件源码 项目:aiolocust 作者: kpidata 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_spawn_zero_locusts(self):
        class MyTaskSet(TaskSet):
            @task
            def my_task(self):
                pass

        class MyTestLocust(Locust):
            task_set = MyTaskSet
            min_wait = 100
            max_wait = 100

        runner = LocalLocustRunner([MyTestLocust], self.options)

        timeout = gevent.Timeout(2.0)
        timeout.start()

        try:
            runner.start_hatching(0, 1, wait=True)
            runner.greenlet.join()
        except gevent.Timeout:
            self.fail("Got Timeout exception. A locust seems to have been spawned, even though 0 was specified.")
        finally:
            timeout.cancel()
agent.py 文件源码 项目:volttron-applications 作者: VOLTTRON 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def remote_setup(self, node):
                if(node == self.zonenum):
                        return

                else:
                        try:
                                Log.info("Connecting to Zone: " + str(node))
                                masterVIP = (self.Config["masternode_z"+str(node)] 
                            + "?serverkey=" + self.Config["serverkey_z"+str(node)] 
                            + "&publickey=" + ks.public + "&secretkey=" + ks.secret)

                                event = gevent.event.Event()
                                masternode = Agent(address=masterVIP, enable_store=False, 
                            identity=self.Config["identity"])
                                masternode.core.onstart.connect(lambda *a, **kw: event.set(),event)
                                gevent.spawn(masternode.core.run)
                                event.wait(timeout=5)
                                self.platforms[node-1] = masternode

                        except gevent.Timeout:
                                Log.exception("Platform Connection Timeout")

    ###Subsribe to leader channel heartbeat
agent.py 文件源码 项目:volttron-applications 作者: VOLTTRON 项目源码 文件源码 阅读 51 收藏 0 点赞 0 评论 0
def remote_setup(self, z):
                if(z == self.zonenum):
                        return

                else:
                        try:
                                Log.info("Connecting to Zone: " + str(z))
                                VIP = self.Config["modelnode_z"+str(z)] + "?serverkey=" + \
                    self.Config["serverkey_z"+str(z)] + "&publickey=" + \
                    ks.public + "&secretkey=" + ks.secret
                                event = gevent.event.Event()
                                node = Agent(address=VIP, enable_store=False, identity=self.Config["identity"])
                                node.core.onstart.connect(lambda *a, **kw: event.set(),event)
                                gevent.spawn(node.core.run)
                                event.wait(timeout=5)
                                self.platforms[z-1] = node
                self.platform_status[z-1] = 1

                        except gevent.Timeout:
                                Log.exception("Platform Connection Timeout")
                self.platform_status[z-1] = 0 #note that platform is down


    #Assert alive for leadership
driver.py 文件源码 项目:volttron-applications 作者: VOLTTRON 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _publish_wrapper(self, topic, headers, message):
        while True:
            try:
                with publish_lock():
                    self.vip.pubsub.publish('pubsub', topic, headers=headers, message=message).get(timeout=10.0)
            except gevent.Timeout:
                _log.warn("Did not receive confirmation of publish to "+topic)
                break
            except Again:
                _log.warn("publish delayed: " + topic + " pubsub is busy")
                gevent.sleep(random.random())
            except VIPError as ex:
                _log.warn("driver failed to publish " + topic + ": " + str(ex))
                break
            else:
                break
VirtualSensor.py 文件源码 项目:lc_cloud 作者: refractionPOINT 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _recvData( self, size, timeout = None ):
        data = None
        timeout = gevent.Timeout( timeout )
        timeout.start()
        try:
            data = ''
            while size > len( data ):
                tmp = self._socket.recv( size - len( data ) )
                if not tmp:
                    raise DisconnectException( 'disconnect while receiving' )
                    break
                data += tmp
        except:
            raise
        finally:
            timeout.cancel()
        return data
AdvancedEndpointProcessor.py 文件源码 项目:lc_cloud 作者: refractionPOINT 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def recvData( self, size, timeout = None ):
        data = None
        timeout = gevent.Timeout( timeout )
        timeout.start()
        try:
            data = ''
            while size > len( data ):
                tmp = self.s.recv( size - len( data ) )
                if not tmp:
                    raise DisconnectException( 'disconnect while receiving' )
                    break
                data += tmp
        except:
            raise
        finally:
            timeout.cancel()
        return data
EndpointProcessor.py 文件源码 项目:lc_cloud 作者: refractionPOINT 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def recvData( self, size, timeout = None ):
        data = None
        timeout = gevent.Timeout( timeout )
        timeout.start()
        try:
            data = ''
            while size > len( data ):
                tmp = self.s.recv( size - len( data ) )
                if not tmp:
                    raise DisconnectException( 'disconnect while receiving' )
                    break
                data += tmp
        except:
            raise
        finally:
            timeout.cancel()
        return data
data_agent.py 文件源码 项目:ops_agent 作者: sjqzhang 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def sendData(self, report_data):
        # send data
        #logger.info(self.easy_sock.socket.getpeername())
        #print report_data
        ret, _ = self.session.send_raw_report(report_data,version = b'\x0E')
        if ret != 0:
            return ret

        # wait response
        ret = 1
        with gevent.Timeout(3, False):
            ret, _ = self.session.recv()

        # result
        return ret

    #@profile
chorus.py 文件源码 项目:wade 作者: chartbeat-labs 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _patch_client_for_gevent(self):
        try:
            import gevent
            import gevent.monkey
        except ImportError:
            gevent_enabled = False
        else:
            gevent_enabled = bool(gevent.monkey.saved)

        if gevent_enabled:
            self._Timeout = gevent.Timeout
            self._sleep = gevent.sleep
            self._get_value_event = lambda: gevent.event.AsyncResult()
        else:
            self._Timeout = ValueEventTimeout
            self._sleep = lambda _: None
            self._get_value_event = self._ensure_value_event
core.py 文件源码 项目:zanph 作者: zanph 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _wait_write(self):
        assert self.__writable.ready(), "Only one greenlet can be waiting on this event"
        self.__writable = AsyncResult()
        # timeout is because libzmq cannot be trusted to properly signal a new send event:
        # this is effectively a maximum poll interval of 1s
        tic = time.time()
        dt = self._gevent_bug_timeout
        if dt:
            timeout = gevent.Timeout(seconds=dt)
        else:
            timeout = None
        try:
            if timeout:
                timeout.start()
            self.__writable.get(block=True)
        except gevent.Timeout as t:
            if t is not timeout:
                raise
            toc = time.time()
            # gevent bug: get can raise timeout even on clean return
            # don't display zmq bug warning for gevent bug (this is getting ridiculous)
            if self._debug_gevent and timeout and toc-tic > dt and \
                    self.getsockopt(zmq.EVENTS) & zmq.POLLOUT:
                print("BUG: gevent may have missed a libzmq send event on %i!" % self.FD, file=sys.stderr)
        finally:
            if timeout:
                timeout.cancel()
            self.__writable.set()
core.py 文件源码 项目:zanph 作者: zanph 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _wait_read(self):
        assert self.__readable.ready(), "Only one greenlet can be waiting on this event"
        self.__readable = AsyncResult()
        # timeout is because libzmq cannot always be trusted to play nice with libevent.
        # I can only confirm that this actually happens for send, but lets be symmetrical
        # with our dirty hacks.
        # this is effectively a maximum poll interval of 1s
        tic = time.time()
        dt = self._gevent_bug_timeout
        if dt:
            timeout = gevent.Timeout(seconds=dt)
        else:
            timeout = None
        try:
            if timeout:
                timeout.start()
            self.__readable.get(block=True)
        except gevent.Timeout as t:
            if t is not timeout:
                raise
            toc = time.time()
            # gevent bug: get can raise timeout even on clean return
            # don't display zmq bug warning for gevent bug (this is getting ridiculous)
            if self._debug_gevent and timeout and toc-tic > dt and \
                    self.getsockopt(zmq.EVENTS) & zmq.POLLIN:
                print("BUG: gevent may have missed a libzmq recv event on %i!" % self.FD, file=sys.stderr)
        finally:
            if timeout:
                timeout.cancel()
            self.__readable.set()
test_socket.py 文件源码 项目:zanph 作者: zanph 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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()
test_device.py 文件源码 项目:zanph 作者: zanph 项目源码 文件源码 阅读 64 收藏 0 点赞 0 评论 0
def test_green_device(self):
            rep = self.context.socket(zmq.REP)
            req = self.context.socket(zmq.REQ)
            self.sockets.extend([req, rep])
            port = rep.bind_to_random_port('tcp://127.0.0.1')
            g = gevent.spawn(zmq.green.device, zmq.QUEUE, rep, rep)
            req.connect('tcp://127.0.0.1:%i' % port)
            req.send(b'hi')
            timeout = gevent.Timeout(3)
            timeout.start()
            receiver = gevent.spawn(req.recv)
            self.assertEqual(receiver.get(2), b'hi')
            timeout.cancel()
            g.kill(block=True)
Connection.py 文件源码 项目:zeronet-debian 作者: bashrc 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def ping(self):
        s = time.time()
        response = None
        with gevent.Timeout(10.0, False):
            try:
                response = self.request("ping")
            except Exception, err:
                self.log("Ping error: %s" % Debug.formatException(err))
        if response and "body" in response and response["body"] == "Pong!":
            self.last_ping_delay = time.time() - s
            return True
        else:
            return False

    # Close connection
test_gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_simpletimeout_expires(self):
        with pipe() as (r, w):
            t = gevent.Timeout.start_new(SHORTTIME)
            try:
                r.get(timeout=t)
                assert False
            except gevent.Timeout as raised_timeout:
                if t is not raised_timeout:
                    raise
test_gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_simpletimeout_expires_contextmanager(self):
        with pipe() as (r, w):
            with gevent.Timeout(SHORTTIME, False) as t:
                r.get(timeout=t)
                assert False
test_gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_simpletimeout_doesnt_expire(self):
        with pipe() as (r, w):
            with gevent.Timeout(SHORTTIME, False) as t:
                w.put('')
                r.get(timeout=t)
                return
        assert False
test_gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def usecase_child_c(reader, syncwriter):
    with syncwriter:
        # Tell partner process that we are up and running!
        syncwriter.put("SYN")
        # Wait for confirmation.
        assert reader.get() == 'SYNACK'
    with reader:
        # Processes are synchronized. CHICKEN must be incoming within no time.
        with gevent.Timeout(SHORTTIME, False) as t:
            assert reader.get(timeout=t) == "CHICKEN"
        # Timeout is invalidated.
        # The write end becomes closed right now.
        with raises(EOFError):
            reader.get()
    sys.exit(5)
gipc.py 文件源码 项目:gipc 作者: jgehrcke 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def get(self, timeout=None):
        """Receive, decode and return data from the pipe. Block
        gevent-cooperatively until data is available or timeout expires. The
        default decoder is ``pickle.loads``.

        :arg timeout: ``None`` (default) or a ``gevent.Timeout``
            instance. The timeout must be started to take effect and is
            canceled when the first byte of a new message arrives (i.e.
            providing a timeout does not guarantee that the method completes
            within the timeout interval).

        :returns: a Python object.

        Raises:
            - :exc:`gevent.Timeout` (if provided)
            - :exc:`GIPCError`
            - :exc:`GIPCClosed`
            - :exc:`pickle.UnpicklingError`

        Recommended usage for silent timeout control::

            with gevent.Timeout(TIME_SECONDS, False) as t:
                reader.get(timeout=t)

        .. warning::

            The timeout control is currently not available on Windows,
            because Windows can't apply select() to pipe handles.
            An ``OSError`` is expected to be raised in case you set a
            timeout.
        """
        self._validate()
        with self._lock:
            if timeout:
                # Wait for ready-to-read event.
                h = gevent.get_hub()
                h.wait(h.loop.io(self._fd, 1))
                timeout.cancel()
            msize, = struct.unpack("!i", self._recv_in_buffer(4).getvalue())
            bindata = self._recv_in_buffer(msize).getvalue()
        return self._decoder(bindata)
file_watch.py 文件源码 项目:kingpin 作者: pinterest 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def timeout_after(secs):
    """Decorator to timeout a function.

    It raises a gevent.Timeout exception after the specified seconds in
    the decorated function. The timeout will work only if the decorated
    function yields, e.g. performing blocking operations through gevent.

    """
    def timeout_enforced(f):
        @wraps(f)
        def g(*args, **kwargs):
            return gevent.with_timeout(secs, f, *args, **kwargs)
        return g
    return timeout_enforced
__init__.py 文件源码 项目:kingpin 作者: pinterest 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _start(self, err_msg, spawn_monit=False):
        if self._is_destroyed:
            return

        self._client = None
        # Increase the session timeout from 10 to 25 seconds.
        try:
            host_list = self.zk_hosts
            client = KazooClient(
                hosts=",".join(host_list),
                timeout=self._get_session_timeout(),
                max_retries=3,
                handler=SequentialGeventHandler())

            # Increase the start timeout to 20 seconds from 15 seconds.
            # Guard this with explicit gevent timeout to protect us from
            # some corner cases where starting client failed to respect
            # start timeout passed in below.
            with gevent.Timeout(seconds=self._get_start_timeout() + 5):
                client.start(timeout=self._get_start_timeout())
            client.ensure_path("/")
            self._last_success_health_check_ts = time.time()
            log.info("Successfully started kazoo client.")
            self._client = client
        except (Exception, gevent.Timeout):
            self._sc.increment("errors.zk.client.start.failure",
                               tags={'host': hostname},
                               sample_rate=1)
            log.exception(err_msg)
        finally:
            if spawn_monit:
                self._monit_greenlet = gevent.spawn(self._monit)
                gevent.sleep(0)


问题


面经


文章

微信
公众号

扫码关注公众号