我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用tornado.options.options.timeout()。
def cull_idle(url, api_token, timeout): """cull idle single-user servers""" auth_header = { 'Authorization': 'token %s' % api_token } req = HTTPRequest(url=url + '/users', headers=auth_header, ) now = datetime.datetime.utcnow() cull_limit = now - datetime.timedelta(seconds=timeout) client = AsyncHTTPClient() resp = yield client.fetch(req) users = json.loads(resp.body.decode('utf8', 'replace')) futures = [] for user in users: last_activity = parse_date(user['last_activity']) if user['server'] and last_activity < cull_limit: app_log.info("Culling %s (inactive since %s)", user['name'], last_activity) req = HTTPRequest(url=url + '/users/%s/server' % user['name'], method='DELETE', headers=auth_header, ) futures.append((user['name'], client.fetch(req))) elif user['server'] and last_activity > cull_limit: app_log.debug("Not culling %s (active since %s)", user['name'], last_activity) for (name, f) in futures: yield f app_log.debug("Finished culling %s", name)
def _rpc_call(self, method, *args): http_client = AsyncHTTPClient() request_id = str(uuid4()) payload = json_encode({'jsonrpc': '2.0', 'id': request_id, 'method': method, 'params': args}) resp = yield http_client.fetch(self.api_root, connect_timeout=options.timeout, request_timeout=options.timeout, method='POST', body=payload) if resp.code != 200: raise UBusServerError(reason='server returns %s' % resp.code) try: LOG.debug('rpc returns: %s' % resp.body) data = json_decode(resp.body) except ValueError: raise UBusServerError(reason='server returns malformed json') else: if data['id'] != request_id: UBusServerError(reason='server did not returns' ' correspond request_id') if 'result' in data: raise Return(data['result']) elif 'error' in data: error = data['error'] raise UBusClientError(code=error['code'], reason=error['message']) else: UBusServerError(reason='server returns malformed json')
def cull_idle(url, api_token, timeout, cull_users=False): """Shutdown idle single-user servers If cull_users, inactive *users* will be deleted as well. """ auth_header = { 'Authorization': 'token %s' % api_token } req = HTTPRequest(url=url + '/users', headers=auth_header, ) now = datetime.datetime.utcnow() cull_limit = now - datetime.timedelta(seconds=timeout) client = AsyncHTTPClient() resp = yield client.fetch(req) users = json.loads(resp.body.decode('utf8', 'replace')) futures = [] @coroutine def cull_one(user, last_activity): """cull one user""" # shutdown server first. Hub doesn't allow deleting users with running servers. if user['server']: app_log.info("Culling server for %s (inactive since %s)", user['name'], last_activity) req = HTTPRequest(url=url + '/users/%s/server' % user['name'], method='DELETE', headers=auth_header, ) yield client.fetch(req) if cull_users: app_log.info("Culling user %s (inactive since %s)", user['name'], last_activity) req = HTTPRequest(url=url + '/users/%s' % user['name'], method='DELETE', headers=auth_header, ) yield client.fetch(req) for user in users: if not user['server'] and not cull_users: # server not running and not culling users, nothing to do continue last_activity = parse_date(user['last_activity']) if last_activity < cull_limit: futures.append((user['name'], cull_one(user, last_activity))) else: app_log.debug("Not culling %s (active since %s)", user['name'], last_activity) for (name, f) in futures: yield f app_log.debug("Finished culling %s", name)