本文整理汇总了Python中async_timeout.timeout方法的典型用法代码示例。如果您正苦于以下问题:Python async_timeout.timeout方法的具体用法?Python async_timeout.timeout怎么用?Python async_timeout.timeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类async_timeout
的用法示例。
在下文中一共展示了async_timeout.timeout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _connect_sentinel
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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
示例2: test_create_no_minsize
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
def test_create_no_minsize(create_pool, server):
pool = await create_pool(
server.tcp_address,
minsize=0, maxsize=1)
assert pool.size == 0
assert pool.freesize == 0
with (await pool):
assert pool.size == 1
assert pool.freesize == 0
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(pool.acquire(),
timeout=0.2)
assert pool.size == 1
assert pool.freesize == 1
示例3: RPCContext
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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
示例4: check_agent_lost
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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.
示例5: __init__
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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
示例6: _connect
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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)
示例7: test_stream_reconnection_enhance_your_calm
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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}
示例8: test_stream_cancel
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
def test_stream_cancel(event_loop):
async def cancel(task):
await asyncio.sleep(0.001)
task.cancel()
async def test_stream_iterations(stream):
async with async_timeout.timeout(0.5):
while True:
await _stream_iteration(stream)
async with aiohttp.ClientSession() as session:
client = peony.client.BasePeonyClient("", "", session=session)
context = peony.stream.StreamResponse(method='GET',
url="http://whatever.com",
client=client)
with context as stream:
with patch.object(stream, '_connect',
side_effect=stream_content):
coro = test_stream_iterations(stream)
task = event_loop.create_task(coro)
cancel_task = event_loop.create_task(cancel(task))
with async_timeout.timeout(1):
await asyncio.wait([task, cancel_task])
示例9: _download_file
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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()
示例10: test_rpc_call
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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
示例11: test_rpc_timeout
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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
示例12: transfer_info
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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 []
示例13: search
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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还贵
示例14: enumerate
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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的情况
示例15: __init__
# 需要导入模块: import async_timeout [as 别名]
# 或者: from async_timeout import timeout [as 别名]
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