Python gevent.Timeout() Examples
The following are 30
code examples of gevent.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
gevent
, or try the search function
.
Example #1
Source File: ws.py From janus-cloud with GNU Affero General Public License v3.0 | 6 votes |
def send_message(self, message, timeout=30): """ send message :param message: object which can be encoded by msg_encoder (by default json encoder) :param timeout: send timeout in second, if timeout, gevent.Timeout exception will be raised :return: """ if self.server_terminated: raise Exception('Already closed: {0}'.format(self)) if self._pingpong_trigger: self._last_active_ts = get_monotonic_time() with gevent.Timeout(seconds=timeout): self.send(self._msg_encoder.encode(message), binary=False) #log.debug("Sent message to {0}: {1}".format(self, self._msg_encoder.encode(message))) # transport session interface methods
Example #2
Source File: __init__.py From steam with MIT License | 6 votes |
def wait_msg(self, event, timeout=None, raises=None): """Wait for a message, similiar to :meth:`.wait_event` :param event: event id :type event: :class:`.EMsg` or :class:`str` :param timeout: seconds to wait before timeout :type timeout: :class:`int` :param raises: On timeout when ``False`` returns :class:`None`, else raise :class:`gevent.Timeout` :type raises: :class:`bool` :return: returns a message or :class:`None` :rtype: :class:`None`, or `proto message` :raises: :class:`gevent.Timeout` """ resp = self.wait_event(event, timeout, raises) if resp is not None: return resp[0]
Example #3
Source File: __init__.py From steam with MIT License | 6 votes |
def send_job_and_wait(self, message, body_params=None, timeout=None, raises=False): """Send a message as a job and wait for the response. .. note:: Not all messages are jobs, you'll have to find out which are which :param message: a message instance :type message: :class:`.Msg`, :class:`.MsgProto` :param body_params: a dict with params to the body (only :class:`.MsgProto`) :type body_params: dict :param timeout: (optional) seconds to wait :type timeout: :class:`int` :param raises: (optional) On timeout if ``False`` return ``None``, else raise :class:`gevent.Timeout` :type raises: :class:`bool` :return: response proto message :rtype: :class:`.Msg`, :class:`.MsgProto` :raises: :class:`gevent.Timeout` """ job_id = self.send_job(message, body_params) response = self.wait_event(job_id, timeout, raises=raises) if response is None: return None return response[0].body
Example #4
Source File: __init__.py From steam with MIT License | 6 votes |
def send_message_and_wait(self, message, response_emsg, body_params=None, timeout=None, raises=False): """Send a message to CM and wait for a defined answer. :param message: a message instance :type message: :class:`.Msg`, :class:`.MsgProto` :param response_emsg: emsg to wait for :type response_emsg: :class:`.EMsg`,:class:`int` :param body_params: a dict with params to the body (only :class:`.MsgProto`) :type body_params: dict :param timeout: (optional) seconds to wait :type timeout: :class:`int` :param raises: (optional) On timeout if ``False`` return ``None``, else raise :class:`gevent.Timeout` :type raises: :class:`bool` :return: response proto message :rtype: :class:`.Msg`, :class:`.MsgProto` :raises: :class:`gevent.Timeout` """ self.send(message, body_params) response = self.wait_event(response_emsg, timeout, raises=raises) if response is None: return None return response[0].body
Example #5
Source File: test_core_cm.py From steam with MIT License | 6 votes |
def test_connect(self, mock_recv, mock_emit): # setup self.conn.connect.return_value = True self.server_list.__len__.return_value = 10 # run cm = CMClient() with gevent.Timeout(2, False): cm.connect(retry=1) gevent.idle() # verify self.conn.connect.assert_called_once_with((127001, 20000)) mock_emit.assert_called_once_with('connected') mock_recv.assert_called_once_with()
Example #6
Source File: test_core_cm.py From steam with MIT License | 6 votes |
def test_connect_auto_discovery_failing(self, mock_recv, mock_emit): # setup self.conn.connect.return_value = True self.server_list.__len__.return_value = 0 # run cm = CMClient() with gevent.Timeout(3, False): cm.connect(retry=1) gevent.idle() # verify self.server_list.bootstrap_from_webapi.assert_called_once_with() self.server_list.bootstrap_from_dns.assert_called_once_with() self.conn.connect.assert_not_called()
Example #7
Source File: server.py From gsmtpd with MIT License | 6 votes |
def handle(self, sock, addr): logger.debug('Incomming connection %s:%s', *addr[:2]) if self.relay and not addr[0] in self.remoteaddr: logger.debug('Not in remoteaddr', *addr[:2]) return try: with Timeout(self.timeout, ConnectionTimeout): sc = SMTPChannel(self, sock, addr, self.data_size_limit) while not sc.closed: sc.handle_read() except ConnectionTimeout: logger.warn('%s:%s Timeouted', *addr[:2]) try: sc.smtp_TIMEOUT() except Exception as err: logger.debug(err) except Exception as err: logger.error(err) # API for "doing something useful with the message"
Example #8
Source File: test_http_concurrent_limit.py From huskar with MIT License | 6 votes |
def test_long_polling_end_no_concurrent_limit( client, client_ip, mocker, test_application, test_application_token): mocker.patch.object(settings, 'CONCURRENT_LIMITER_SETTINGS', { '__default__': { 'ttl': 100, 'capacity': 1, } }) data = { 'service': { test_application.application_name: ['foo'], }, } result = [] for _ in range(3): response = client.post( '/api/data/long-polling', headers={ 'Authorization': test_application_token, }, query_string={'life_span': 1}, content_type='application/json', data=json.dumps(data)) with gevent.Timeout(2): list(response.response) result.append(response.status_code) assert 429 not in result
Example #9
Source File: aceclient.py From HTTPAceProxy with GNU General Public License v3.0 | 6 votes |
def GetAUTH(self): ''' AUTH method ''' try: self._response['HELLOTS'] = AsyncResult() self._write(AceRequest.HELLOBG()) paramsdict = self._response['HELLOTS'].get(timeout=self._responsetimeout) except gevent.Timeout as t: raise AceException('Engine response time %s exceeded. HELLOTS not resived!' % t) try: self._response['AUTH'] = AsyncResult() self._response['NOTREADY'] = AsyncResult() self._write(AceRequest.READY(paramsdict.get('key'), self._product_key)) auth_level = self._response['AUTH'].get(timeout=self._responsetimeout) if int(paramsdict.get('version_code', 0)) >= 3003600: self._write(AceRequest.SETOPTIONS({'use_stop_notifications': '1'})) except gevent.Timeout as t: if self._response['NOTREADY'].value: errmsg = 'Engine response time %s exceeded. %s resived!' % (t, self._response['NOTREADY'].value) else: errmsg = 'Engine response time %s exceeded. AUTH not resived!' % t raise AceException(errmsg)
Example #10
Source File: __init__.py From kingpin with Apache License 2.0 | 6 votes |
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.")
Example #11
Source File: test_gipc.py From gipc with MIT License | 6 votes |
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
Example #12
Source File: custom_gevent_pool_executor.py From distributed_framework with Apache License 2.0 | 6 votes |
def gevent_timeout_deco(timeout_t): def _gevent_timeout_deco(f): def __gevent_timeout_deceo(*args, **kwargs): timeout = gevent.Timeout(timeout_t, ) timeout.start() result = None try: result = f(*args, **kwargs) except gevent.Timeout as t: logger_gevent_timeout_deco.error(f'函数 {f} 运行超过了 {timeout_t} 秒') if t is not timeout: nb_print(t) # raise # not my timeout finally: timeout.close() return result return __gevent_timeout_deceo return _gevent_timeout_deco
Example #13
Source File: home.py From bluemix-python-eve-sample with Apache License 2.0 | 6 votes |
def populate(): print("Begin MAC Address population ...") try: with Timeout(300): oui.update() except Exception as e: print(e) print(traceback.format_exc()) # Response needed for local execution which runs shorter than 2 mins finally: data = { 'message': 'Whoaa! MacReduce Data Populated/Refreshed' } message = json.dumps(data) print(message) resp = Response(message, status=200, mimetype='application/json') return resp
Example #14
Source File: engine.py From NoXss with MIT License | 6 votes |
def request_and_verify(case): vul = case.vul method = case.method url = case.url headers = case.headers body = case.body args = case.args old_param = args[2] old_value = args[3] print 'Verify case use:\n%s' % url # time out with gevent.Timeout(20, False)as t: resp = make_request(method, url, headers, body) if resp: if Verify.verify(resp, args): poc = gen_poc(method, url, body, old_param, old_value) print_warn('Found %s in %s' % (vul, poc)) result = (vul, url, poc) return result # count++ when error happened else: Verify.ERROR_COUNT += 1
Example #15
Source File: engine.py From NoXss with MIT License | 6 votes |
def gen_traffic(self, url): domain = get_domain_from_url(url) # add cookie to DEFAULT_HEADER cookie = get_cookie(domain) self.DEFAULT_HEADER['Cookie'] = cookie # add referer self.DEFAULT_HEADER['Referer'] = 'https"//' + domain + '/' request = HttpRequest(method='GET', url=url, headers=self.DEFAULT_HEADER, body='') req = urllib2.Request(url=url, headers=self.DEFAULT_HEADER) with gevent.Timeout(10, False)as t: try: resp = urllib2.urlopen(req) except urllib2.URLError, e: REQUEST_ERROR.append(('gen_traffic()', url, e.reason)) except CertificateError: REQUEST_ERROR.append(('gen_traffic()', url, 'ssl.CertificateError'))
Example #16
Source File: oui.py From bluemix-python-eve-sample with Apache License 2.0 | 5 votes |
def update(): print("Deleting (reset) Mongo Collection named 'mac'") delete("mac") with Timeout(5, False): oui = urllib2.urlopen(OUI_URL, timeout=240) # code, oui = _sendGetRequest(OUI_URL, {}, {}) # print "IEEE Response Code was a " + str(code) count = 1 for totalcount, line in enumerate(oui, start=1): macHexVendor = re.match("^.*(" + OUI_HEX_REGEX + ").*" + "hex" + ".*?([A-Z].*)$", line) if macHexVendor: count += 1 macEntry = { "base16": macHexVendor.group(1).replace(OUI_MATCH, ""), "hex": macHexVendor.group(1).replace(OUI_MATCH, OUI_REPLACE), "organization": macHexVendor.group(2) } post_internal("mac", macEntry) if not VCAP_CONFIG: print(macHexVendor.group(1).replace(OUI_MATCH, OUI_REPLACE) + ", " + macHexVendor.group(2)) print("Number of MAC Entries matched: " + str(count)) return "" # requests status codes: # https://github.com/kennethreitz/requests/blob/master/requests/status_codes.py
Example #17
Source File: infractions.py From rowboat with MIT License | 5 votes |
def mkick(self, event, args): members = [] for user_id in args.users: member = event.guild.get_member(user_id) if not member: # TODO: this sucks, batch these raise CommandFail('failed to kick {}, user not found'.format(user_id)) if not self.can_act_on(event, member.id, throw=False): raise CommandFail('failed to kick {}, invalid permissions'.format(user_id)) members.append(member) msg = event.msg.reply('Ok, kick {} users for `{}`?'.format(len(members), args.reason or 'no reason')) msg.chain(False).\ add_reaction(GREEN_TICK_EMOJI).\ add_reaction(RED_TICK_EMOJI) try: mra_event = self.wait_for_event( 'MessageReactionAdd', message_id=msg.id, conditional=lambda e: ( e.emoji.id in (GREEN_TICK_EMOJI_ID, RED_TICK_EMOJI_ID) and e.user_id == event.author.id )).get(timeout=10) except gevent.Timeout: return finally: msg.delete() if mra_event.emoji.id != GREEN_TICK_EMOJI_ID: return for member in members: Infraction.kick(self, event, member, args.reason) raise CommandSuccess('kicked {} users'.format(len(members)))
Example #18
Source File: __init__.py From kingpin with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: file_watch.py From kingpin with Apache License 2.0 | 5 votes |
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
Example #20
Source File: test_long_polling.py From huskar with MIT License | 5 votes |
def test_session_with_max_life_span( test_application_name, mocker, switch_on, test_application_token, client, max_life_span, jitter, life_span, timeout, exclude): def fake_switch(name, default=True): if name == SWITCH_ENABLE_LONG_POLLING_MAX_LIFE_SPAN: return switch_on return default mocker.patch.object(switch, 'is_switched_on', fake_switch) mocker.patch.object(settings, 'LONG_POLLING_MAX_LIFE_SPAN', max_life_span) mocker.patch.object(settings, 'LONG_POLLING_LIFE_SPAN_JITTER', jitter) if exclude: mocker.patch.object( settings, 'LONG_POLLING_MAX_LIFE_SPAN_EXCLUDE', [test_application_name]) else: mocker.patch.object( settings, 'LONG_POLLING_MAX_LIFE_SPAN_EXCLUDE', []) url = '/api/data/long_poll' data = json.dumps({ 'config': {test_application_name: ['foo']}, 'switch': {test_application_name: ['bar']}, 'service': {test_application_name: ['test']}, }) headers = {'Authorization': test_application_token} r = client.post( url, content_type='application/json', data=data, query_string={'life_span': life_span}, headers=headers) assert r.status_code == 200, r.data with gevent.Timeout(timeout): for _ in r.response: pass
Example #21
Source File: db.py From huskar with MIT License | 5 votes |
def close(self): with gevent.Timeout(5): super(RoutingSession, self).close()
Example #22
Source File: __init__.py From kingpin with Apache License 2.0 | 5 votes |
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.")
Example #23
Source File: net_gevent.py From rethinkdb-python with Apache License 2.0 | 5 votes |
def close(self, noreply_wait=False, token=None, exception=None): self._closing = True if exception is not None: err_message = "Connection is closed (%s)." % str(exception) else: err_message = "Connection is closed." # Cursors may remove themselves when errored, so copy a list of them for cursor in list(self._cursor_cache.values()): cursor._error(err_message) for query, async_res in iter(self._user_queries.values()): async_res.set_exception(RqlDriverError(err_message)) self._user_queries = {} self._cursor_cache = {} if noreply_wait: noreply = net.Query(pQuery.NOREPLY_WAIT, token, None, None) self.run_query(noreply, False) try: self._socket.close() except OSError: pass # TODO: make connection recoverable if interrupted by a user's gevent.Timeout?
Example #24
Source File: imap.py From mailur with GNU General Public License v3.0 | 5 votes |
def idle(con, handlers, timeout=None): def match(): for code, handler in handlers.items(): typ, dat = con._untagged_response('OK', [None], code) if not dat[-1]: continue handler(dat) def inner(tag): with Timeout(timeout): res = con._get_response() if res: log.debug('received: %r', res.decode()) bad = con.tagged_commands[tag] if bad: raise Error(bad) match() match() log.info('start idling %s...' % con) with _cmd(con, 'IDLE') as (tag, start, complete): clean_pool() start(CRLF) while 1: try: inner(tag) except Timeout: log.debug('timeout reached: %ss', timeout) return
Example #25
Source File: sink.py From scales with MIT License | 5 votes |
def _AsyncProcessTransaction(self, sink_stack, msg, deadline): method = getattr(self._client, msg.method) with self._varz.transport_latency.Measure(): gtimeout = None try: if deadline: timeout = deadline - time.time() if timeout < 0: raise gevent.Timeout() gtimeout = gevent.Timeout.start_new(timeout) else: gtimeout = NoopTimeout() self._varz.messages_sent() ret = method(*msg.args, **msg.kwargs) msg = MethodReturnMessage(ret) gtimeout.cancel() self._processing = None gevent.spawn(sink_stack.AsyncProcessResponseMessage, msg) except gevent.Timeout: # pylint: disable=E0712 err = TimeoutError() self._client.connection_pool.disconnect() self._processing = None gevent.spawn(sink_stack.AsyncProcessResponseMessage, MethodReturnMessage(error=err)) except Exception as err: self._processing = None if gtimeout: gtimeout.cancel() self._Fault(err) gevent.spawn(sink_stack.AsyncProcessResponseMessage, MethodReturnMessage(error=err)) finally: self._varz.messages_recv()
Example #26
Source File: core.py From pySINDy with MIT License | 5 votes |
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()
Example #27
Source File: core.py From pySINDy with MIT License | 5 votes |
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()
Example #28
Source File: test_device.py From pySINDy with MIT License | 5 votes |
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)
Example #29
Source File: __init__.py From pySINDy with MIT License | 5 votes |
def tearDown(self): contexts = set([self.context]) while self.sockets: sock = self.sockets.pop() contexts.add(sock.context) # in case additional contexts are created sock.close() try: gevent.joinall([gevent.spawn(ctx.term) for ctx in contexts], timeout=2, raise_error=True) except gevent.Timeout: raise RuntimeError("context could not terminate, open sockets likely remain in test")
Example #30
Source File: test_socket.py From pySINDy with MIT License | 5 votes |
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()