我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用gevent.Timeout()。
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
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
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
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
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.")
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
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.')
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
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)
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()
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
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
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
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
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
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
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
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()
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()
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()
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)
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
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
def test_simpletimeout_expires_contextmanager(self): with pipe() as (r, w): with gevent.Timeout(SHORTTIME, False) as t: r.get(timeout=t) assert False
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
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)
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)
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
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)
def _dispatch_client_change_callback(self, client): if self._is_destroyed: return log.info("Start dispatching client change callback.") for callback in self._client_callbacks: try: callback(client) except (Exception, gevent.Timeout): self._sc.increment("errors.zk.client.change_callback.failure", tags={'host': hostname}, sample_rate=1) log.exception("Failed to exec client change callback.")
def stop(self): """ Stop the node. """ self.alarm.stop_async() self.protocol.stop_and_wait() wait_for = [self.alarm] wait_for.extend(self.protocol.greenlets) wait_for.extend(self.greenlet_task_dispatcher.stop()) # We need a timeout to prevent an endless loop from trying to # contact the disconnected client gevent.wait(wait_for, timeout=self.shutdown_timeout) # Filters must be uninstalled after the alarm task has stopped. Since # the events are polled by a alarm task callback, if the filters are # uninstalled before the alarm task is fully stopped the callback # `poll_blockchain_events` will fail. # # We need a timeout to prevent an endless loop from trying to # contact the disconnected client try: with gevent.Timeout(self.shutdown_timeout): self.blockchain_events.uninstall_all_event_listeners() except gevent.timeout.Timeout: pass # save the state after all tasks are done if self.serialization_file: save_snapshot(self.serialization_file, self) if self.db_lock is not None: self.db_lock.release()
def timeout_ctx(self): return gevent.Timeout(self.cfg.keepalive, False)
def _wait(self, action=None, timeout=None): """ ???service_name??service?? :param timeout: :type timeout: float :return: """ remain = timeout waiter = Waiter() self._oc.add_waiter(self.service_name, waiter) try: while True: with Timeout(remain, _TimeOut): start = time.time() cur_action = waiter.get() remain = remain - (time.time() - start) if action is None: # ??????????? break elif action == cur_action: # ???????? break elif remain < 0.001: # ????????1ms raise _TimeOut else: continue except _TimeOut: # ???? return False except Exception as e: raise err.OctpParamError('catch unexpect error: %s. more: %s', e, traceback.format_exc()) else: return True finally: self._oc.del_waiter(self.service_name, waiter)
def __call__(self, func): func_logger = logging.getLogger(func.__module__) @functools.wraps(func) def wrapper(*args, **kwargs): start = time.time() request = kwargs.get('request') if request: _of = getattr(func, 'original', func) if 'request' not in _of.__code__.co_varnames: del kwargs['request'] response = kwargs.get('response') if response: _of = getattr(func, 'original', func) if 'response' not in _of.__code__.co_varnames: del kwargs['response'] try: if self.timeout: with gevent.Timeout(self.timeout): return self._process(func, args, kwargs, request, response) else: return self._process(func, args, kwargs, request, response) except falcon.http_status.HTTPStatus: raise except Exception as e: return self._process_exception_output(e, func_logger, request, response, args, kwargs) finally: execution_time = (time.time() - start) * 1000 self._finish_exec(execution_time, func_logger, args, kwargs, request, func) return self._gevent_wrapper(wrapper)
def __call__(self, *args, **kwargs): with gevent.Timeout(self.timeout): return self._gevent_wrapper(self.app)(*args, **kwargs)
def handle_request(self, *args): # pragma: no cover """ Apply the configured 'timeout' value to each individual request. Note that self.timeout is set to half the configured timeout by the arbiter, so we use the value directly from the config. """ with gevent.Timeout(self.cfg.timeout): return super(GeventWorker, self).handle_request(*args)
def rollback(self): with gevent.Timeout(5): super(RoutingSession, self).rollback()
def close(self): current_transactions = tuple() if self.transaction is not None: current_transactions = self.transaction._iterate_parents() try: with gevent.Timeout(5): super(RoutingSession, self).close() # pylint: disable=E0712 except gevent.Timeout: # pylint: enable=E0712 close_connections(self.engines.itervalues(), current_transactions) raise
def execute(self, **kwargs): """ Runs the fulfillment strategy on the initiator until the conditions are met. :return: """ with gevent.Timeout(self.timeout): result = self.fulfillment.run(self.initiator, self.conditions, **kwargs) result.event_name = self.name return result
def run(self, event_name, event_result_q): """ Execute an individual event. Success: - return a result with 'success' = True Failure: - Raise an exception - Timeout - Return Result with 'success' = False :param event_name: :param event_result_q: :return: """ event = self.events_dict[event_name] try: result = event.execute(event_results=self.event_results) self.event_results.add(result) except (Exception, Timeout) as e: logger.error('%s', { 'message': 'event_execution_error', 'exception': e, 'event_name': event_name, }) logger.error(traceback.format_exc()) return event_result_q.put(EVENT_RESULT.FAILURE) event_result_q.put(result.success())