Python async_timeout.timeout() Examples
The following are 30
code examples of async_timeout.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
async_timeout
, or try the search function
.
Example #1
Source File: test_client_stream.py From grpclib with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_deadline_during_recv_message(): cs = ClientStream(timeout=0.01, send_type=DummyRequest, recv_type=DummyReply) with pytest.raises(ErrorDetected): with async_timeout.timeout(5) as safety_timeout: async with cs.client_stream as stream: await stream.send_message(DummyRequest(value='ping'), end=True) events = cs.client_conn.to_server_transport.events() stream_id = events[-1].stream_id cs.client_conn.server_h2c.send_headers( stream_id, [(':status', '200'), ('content-type', 'application/grpc+proto')], ) cs.client_conn.server_flush() await stream.recv_initial_metadata() try: await stream.recv_message() except asyncio.TimeoutError: if safety_timeout.expired: raise else: raise ErrorDetected()
Example #2
Source File: stream.py From peony-twitter with MIT License | 6 votes |
def __init__(self, client, session=None, loads=data_processing.loads, timeout=10, **kwargs): self.client = client self.session = session self.loads = loads self.timeout = timeout self.kwargs = kwargs self.response = None self._reconnecting = False self._state = NORMAL self._error_timeout = 0
Example #3
Source File: stream.py From peony-twitter with MIT License | 6 votes |
def _connect(self): """ Connect to the stream Returns ------- asyncio.coroutine The streaming response """ logger.debug("connecting to the stream") await self.client.setup if self.session is None: self.session = self.client._session kwargs = await self.client.headers.prepare_request(**self.kwargs) request = self.client.error_handler(self.session.request) return await request(timeout=0, **kwargs)
Example #4
Source File: pool.py From aioredis with MIT License | 6 votes |
def discover_slave(self, service, timeout, **kwargs): """Perform Slave discovery for specified service.""" # TODO: use kwargs to change how slaves are picked up # (eg: round-robin, priority, random, etc) idle_timeout = timeout pools = self._pools[:] for sentinel in pools: try: with async_timeout(timeout): address = await self._get_slave_address( sentinel, service) # add **kwargs pool = self._slaves[service] with async_timeout(timeout), \ contextlib.ExitStack() as stack: conn = await pool._create_new_connection(address) stack.callback(conn.close) await self._verify_service_role(conn, 'slave') stack.pop_all() return conn except asyncio.CancelledError: raise except asyncio.TimeoutError: continue except DiscoverError: await asyncio.sleep(idle_timeout) continue except RedisError as err: raise SlaveReplyError("Service {} error".format(service), err) except Exception: await asyncio.sleep(idle_timeout) continue raise SlaveNotFoundError("No slave found for {}".format(service))
Example #5
Source File: test_stream.py From peony-twitter with MIT License | 6 votes |
def test_stream_reconnection_enhance_your_calm(): async def dummy(*args, **kwargs): pass turn = -1 async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_calm): with patch.object(peony.stream.asyncio, 'sleep', side_effect=dummy): async for data in stream: assert stream._state == ENHANCE_YOUR_CALM turn += 1 if turn >= 100: break if turn == 0: assert data == {'connected': True} elif turn % 2 == 1: timeout = ENHANCE_YOUR_CALM_TIMEOUT * 2**(turn // 2) assert data == {'reconnecting_in': timeout, 'error': None} else: assert data == {'stream_restart': True}
Example #6
Source File: manifest.py From pydest with MIT License | 6 votes |
def _download_file(self, url, name): """Async file download Args: url (str): The URL from which to download the file name (str): The name to give to the downloaded file """ with async_timeout.timeout(10): async with self.api.session.get(url) as response: filename = os.path.basename(name) with open(filename, 'wb') as f_handle: while True: chunk = await response.content.read(1024) if not chunk: break f_handle.write(chunk) return await response.release()
Example #7
Source File: test_rpc.py From aioamqp_consumer with MIT License | 6 votes |
def test_rpc_call( rpc_client_factory, rpc_server_close, amqp_queue_name, ): fut = asyncio.Future() @RpcMethod.init(amqp_queue_name) async def test_method(): fut.set_result(True) await rpc_server_close(test_method, amqp_queue_name) client = await rpc_client_factory() resp = await client.call(test_method()) assert resp is None assert not client._map await client.close() async with timeout(0.1): assert await fut
Example #8
Source File: test_rpc.py From aioamqp_consumer with MIT License | 6 votes |
def test_rpc_timeout( rpc_client_close, rpc_server_close, amqp_queue_name, ): fut = asyncio.Future() @RpcMethod.init(amqp_queue_name) async def test_method(): await asyncio.sleep(0.2) fut.set_result(True) server = await rpc_server_close(test_method, amqp_queue_name) client = await rpc_client_close() with pytest.raises(asyncio.TimeoutError): await client.wait(test_method(), timeout=0.1) await server.join() async with timeout(0.2): assert await fut
Example #9
Source File: __init__.py From ESD with GNU General Public License v3.0 | 6 votes |
def transfer_info(self): ret_zones = list() try: nss = dns.resolver.query(self.domain, 'NS') nameservers = [str(ns) for ns in nss] ns_addr = dns.resolver.query(nameservers[0], 'A') # dnspython 的 bug,需要设置 lifetime 参数 zones = dns.zone.from_xfr(dns.query.xfr(ns_addr, self.domain, relativize=False, timeout=2, lifetime=2), check_origin=False) names = zones.nodes.keys() for n in names: subdomain = '' for t in range(0, len(n) - 1): if subdomain != '': subdomain += '.' subdomain += str(n[t].decode()) if subdomain != self.domain: ret_zones.append(subdomain) return ret_zones except BaseException: return []
Example #10
Source File: __init__.py From ESD with GNU General Public License v3.0 | 6 votes |
def search(self): result = list() url = self.base_url.format(email=self.email, key=self.fkey, domain=self.domain) try: resp = requests.Session().get(url, headers=self.headers, timeout=self.timeout) json_resp = json.loads(resp.text) for res in json_resp['results']: domain = urlparse.urlparse(res[0]).netloc result.append(domain.rsplit(self.domain, 1)[0].strip('.')) except Exception as e: result = [] return result # Zoomeye的效果还可以,但是比fofa还贵
Example #11
Source File: __init__.py From ESD with GNU General Public License v3.0 | 6 votes |
def enumerate(self): flag = True num = 1 result = list() while flag: response = self.search(num) if response is None or 'error' in response.keys(): # print(response) flag = False else: match_list = response["matches"] for block in match_list: domain = block['site'] result.append(domain.rsplit(self.domain, 1)[0].strip('.')) num = num + 1 return result # censys的接口有点不稳定,经常出现timeout的情况
Example #12
Source File: __init__.py From ESD with GNU General Public License v3.0 | 6 votes |
def check(self, dns): logger.info("Checking if DNS server {dns} is available".format(dns=dns)) msg = b'\x5c\x6d\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x05baidu\x03com\x00\x00\x01\x00\x01' sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(3) repeat = { 1: 'first', 2: 'second', 3: 'third' } for i in range(3): logger.info("Sending message to DNS server a {times} time".format(times=repeat[i + 1])) sock.sendto(msg, (dns, 53)) try: sock.recv(4096) break except socket.timeout as e: logger.warning('Failed!') if i == 2: return False return True
Example #13
Source File: animal.py From yui with GNU Affero General Public License v3.0 | 6 votes |
def get_cat_image_url(timeout: float) -> str: api_url = 'http://thecatapi.com/api/images/get' async with aiohttp.ClientSession() as session: while True: try: async with session.get( api_url, params={'format': 'xml', 'type': 'jpg,png'} ) as res: if res.status != 200: raise APIServerError xml_result = await res.read() tree = etree.fromstring(xml_result) url = tree.find('data/images/image/url').text except aiohttp.client_exceptions.ServerDisconnectedError: await asyncio.sleep(0.1) continue try: async with async_timeout.timeout(timeout=timeout): async with session.get(url) as res: async with res: if res.status == 200: return url except (aiohttp.ClientConnectorError, asyncio.TimeoutError): continue
Example #14
Source File: animal.py From yui with GNU Affero General Public License v3.0 | 6 votes |
def get_dog_image_url(timeout: float) -> str: api_url = 'https://dog.ceo/api/breeds/image/random' async with aiohttp.ClientSession() as session: while True: try: async with session.get(api_url) as res: if res.status != 200: raise APIServerError data = await res.json(loads=json.loads) url = data['message'] except aiohttp.client_exceptions.ServerDisconnectedError: await asyncio.sleep(0.1) continue try: async with async_timeout.timeout(timeout=timeout): async with session.get(url) as res: async with res: if res.status == 200: return url except (aiohttp.ClientConnectorError, asyncio.TimeoutError): continue
Example #15
Source File: sub.py From yui with GNU Affero General Public License v3.0 | 6 votes |
def get_ohli_caption_list(i, timeout: float) -> Set[Sub]: result: Set[Sub] = set() async with async_timeout.timeout(timeout): async with aiohttp.ClientSession() as session: async with session.get( f'https://ohli.moe/timetable/cap?i={i}' ) as resp: data = await resp.json(loads=json.loads) for sub in data: episode_num = sub['s'] if int(math.ceil(episode_num)) == int(episode_num): episode_num = int(episode_num) result.add( Sub( maker=sub['n'], episode_num=episode_num, url=sub['a'], released_at=sub['d'], ) ) return result
Example #16
Source File: dialog.py From aiosip with Apache License 2.0 | 6 votes |
def close(self, timeout=None): if not self._closed: self._closed = True msg = None if self._state == CallState.Terminated: msg = self._prepare_request('BYE') elif self._state != CallState.Completed: msg = self._prepare_request('CANCEL') if msg: transaction = UnreliableTransaction(self, original_msg=msg, loop=self.app.loop) self.transactions[msg.method][msg.cseq] = transaction try: async with Timeout(timeout): await transaction.start() finally: self._close() self._close()
Example #17
Source File: communication.py From modmail with GNU Affero General Public License v3.0 | 6 votes |
def handler(self, action, expected_count, args=None, _timeout=1, scope="bot", cluster=None): command_id = f"{uuid4()}" self._messages[command_id] = [] payload = {"scope": scope, "action": action, "command_id": command_id} if cluster: payload["id"] = cluster if args: payload["args"] = json.dumps(args) await self.bot.redis.execute("PUBLISH", self.ipc_channel, json.dumps(payload)) try: async with timeout(_timeout): while len(self._messages[command_id]) < expected_count: await asyncio.sleep(0.05) except asyncio.TimeoutError: pass return self._messages.pop(command_id, None)
Example #18
Source File: pool.py From aioredis with MIT License | 6 votes |
def _connect_sentinel(self, address, timeout, pools): """Try to connect to specified Sentinel returning either connections pool or exception. """ try: with async_timeout(timeout): pool = await create_pool( address, minsize=1, maxsize=2, parser=self._parser_class, ) pools.append(pool) return pool except asyncio.TimeoutError as err: sentinel_logger.debug( "Failed to connect to Sentinel(%r) within %ss timeout", address, timeout) return err except Exception as err: sentinel_logger.debug( "Error connecting to Sentinel(%r): %r", address, err) return err
Example #19
Source File: test_client_stream.py From grpclib with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_deadline_during_send_message(): cs = ClientStream(timeout=0.01, send_type=DummyRequest, recv_type=DummyReply) with pytest.raises(ErrorDetected): with async_timeout.timeout(5) as safety_timeout: async with cs.client_stream as stream: await stream.send_request() cs.client_conn.client_proto.connection.write_ready.clear() try: await stream.send_message(DummyRequest(value='ping'), end=True) except asyncio.TimeoutError: if safety_timeout.expired: raise else: raise ErrorDetected()
Example #20
Source File: test_client_stream.py From grpclib with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_deadline_during_recv_initial_metadata(): cs = ClientStream(timeout=0.01, send_type=DummyRequest, recv_type=DummyReply) with pytest.raises(ErrorDetected): with async_timeout.timeout(5) as safety_timeout: async with cs.client_stream as stream: await stream.send_message(DummyRequest(value='ping'), end=True) try: await stream.recv_initial_metadata() except asyncio.TimeoutError: if safety_timeout.expired: raise else: raise ErrorDetected()
Example #21
Source File: __init__.py From ESD with GNU General Public License v3.0 | 6 votes |
def __init__(self, base_url, domain, q, verbose, proxy): multiprocessing.Process.__init__(self) self.lock = threading.Lock() self.q = q self.subdomains = [] self.base_url = base_url self.domain = domain self.session = requests.Session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip', } self.timeout = 30 self.verbose = verbose self.proxy = proxy
Example #22
Source File: animal.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def cat(bot, event: Message, timeout: float): """ 냥냥이 짤을 수급합니다. 쿨타임은 일반 채널 30분, DM 3분입니다. `{PREFIX}cat`: 냐짤 수급 """ cat_say = functools.partial( bot.api.chat.postMessage, channel=event.channel, as_user=False, username='냥짤의 요정', icon_url='https://i.imgur.com/hIBJUMI.jpg', ) now_dt = now() if event.channel.id in cat.last_call: last_call = cat.last_call[event.channel.id] if isinstance(event.channel, DirectMessageChannel): cooltime = DM_COOLTIME else: cooltime = DEFAULT_COOLTIME if now_dt - last_call < cooltime: fine = last_call + cooltime await cat_say( text=( f'아직 쿨타임이다냥! ' f"{fine.strftime('%H시 %M분')} 이후로 다시 시도해보라냥!" ) ) return try: url = await get_cat_image_url(timeout) except APIServerError: await cat_say(text='냥냥이 API 서버의 상태가 좋지 않다냥! 나중에 다시 시도해보라냥!') return cat.last_call[event.channel.id] = now_dt await cat_say(text=url)
Example #23
Source File: animal.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def get_fox_image_url(timeout: float) -> str: url = 'http://fox-info.net/fox-gallery' async with async_timeout.timeout(timeout=timeout): async with aiohttp.ClientSession() as session: async with session.get(url) as resp: data = await resp.text() h = html.fromstring(data) image_els = h.cssselect('#gallery-1 img.attachment-thumbnail') try: return str(image_els[0].get('src')) except IndexError: raise APIServerError
Example #24
Source File: registry.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def RPCContext(addr, timeout=None, *, order_key: str = None): global agent_peers peer = agent_peers.get(addr, None) if peer is None: peer = PeerInvoker( connect=ZeroMQAddress(addr), transport=ZeroMQRPCTransport, serializer=msgpack.packb, deserializer=msgpack.unpackb, ) await peer.__aenter__() agent_peers[addr] = peer try: with _timeout(timeout): okey_token = peer.call.order_key.set('') try: yield peer finally: peer.call.order_key.reset(okey_token) except RPCUserError as orig_exc: raise AgentError(orig_exc.name, orig_exc.args) except Exception: raise
Example #25
Source File: sub.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def get_annissia_caption_list_json(i, timeout: float) -> List[Dict]: async with async_timeout.timeout(timeout): async with aiohttp.ClientSession() as session: async with session.get( f'https://www.anissia.net/anitime/cap?i={i}' ) as resp: return json.loads(await resp.text())
Example #26
Source File: dialog.py From aiosip with Apache License 2.0 | 5 votes |
def request(self, method, contact_details=None, headers=None, payload=None, timeout=None): msg = self._prepare_request(method, contact_details, headers, payload) if msg.method != 'ACK': async with Timeout(timeout): return await self.start_unreliable_transaction(msg) else: self.peer.send_message(msg)
Example #27
Source File: pool_test.py From aioredis with MIT License | 5 votes |
def test_pool_idle_close(create_pool, start_server, caplog): server = start_server('idle') conn = await create_pool(server.tcp_address, minsize=2) ok = await conn.execute("config", "set", "timeout", 1) assert ok == b'OK' caplog.clear() with caplog.at_level('DEBUG', 'aioredis'): # wait for either disconnection logged or test timeout reached. while len(caplog.record_tuples) < 2: await asyncio.sleep(.5) expected = [ ('aioredis', logging.DEBUG, 'Connection has been closed by server, response: None'), ('aioredis', logging.DEBUG, 'Connection has been closed by server, response: None'), ] if BPO_34638: expected += [ ('asyncio', logging.ERROR, 'An open stream object is being garbage collected; ' 'call "stream.close()" explicitly.'), ('asyncio', logging.ERROR, 'An open stream object is being garbage collected; ' 'call "stream.close()" explicitly.')] # The order in which logs are collected differs each time. assert sorted(caplog.record_tuples) == sorted(expected) # On CI this test fails from time to time. # It is possible to pick 'unclosed' connection and send command, # however on the same loop iteration it gets closed and exception is raised assert (await conn.execute('ping')) == b'PONG'
Example #28
Source File: aiohttp_client.py From pook with MIT License | 5 votes |
def fetch(session, url, data): with async_timeout.timeout(10): async with session.get(url, data=data) as res: print('Status:', res.status) print('Headers:', res.headers) print('Body:', await res.text())
Example #29
Source File: session.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def check_agent_lost(app, interval): try: now = datetime.now(tzutc()) timeout = timedelta(seconds=app['config']['manager']['heartbeat-timeout']) async def _check_impl(): async for agent_id, prev in app['redis_live'].ihscan('last_seen'): prev = datetime.fromtimestamp(float(prev), tzutc()) if now - prev > timeout: await app['event_dispatcher'].produce_event( 'instance_terminated', ('agent-lost', ), agent_id=agent_id) await redis.execute_with_retries(lambda: _check_impl()) except asyncio.CancelledError: pass # NOTE: This event is ignored during the grace period.
Example #30
Source File: conftest.py From raven-aiohttp with BSD 3-Clause "New" or "Revised" License | 5 votes |
def wait(event_loop): @asyncio.coroutine def do_wait(transport, timeout=1): if isinstance(transport, QueuedAioHttpTransport): coro = transport._queue.join() elif isinstance(transport, AioHttpTransport): coro = asyncio.gather(*transport._tasks, loop=event_loop) else: raise NotImplementedError with async_timeout.timeout(timeout, loop=event_loop): yield from coro return do_wait