如果您想对aiohttp客户端使用超时,请使用标准异步方法: asyncio.wait_for(client.get(url),10)
但这并不能处理DNS超时,我猜这是由操作系统处理的。另外,with aiohttp.Timeout不处理OS查找。
在异步回购上进行了讨论,没有最后的结论,而Saghul已经做出了奥登,但是我不知道如何将它混合到aiohttp中,以及这是否允许asyncio.wait_for功能。
Testcase (在我的linux机器上花费20秒):
async def fetch(url):
url = 'http://alicebluejewelers.com/'
with aiohttp.Timeout(0.001):
resp = await aiohttp.get(url)发布于 2016-01-10 03:12:05
Timeout按预期工作,但不幸的是,您的示例挂起了python关机过程:它等待后台线程的终止,后者执行DNS查找。
作为一种解决方案,我可以建议使用aiodns进行手动IP解析:
import asyncio
import aiohttp
import aiodns
async def fetch():
dns = 'alicebluejewelers.com'
# dns = 'google.com'
with aiohttp.Timeout(1):
ips = await resolver.query(dns, 'A')
print(ips)
url = 'http://{}/'.format(ips[0].host)
async with aiohttp.get(url) as resp:
print(resp.status)
loop = asyncio.get_event_loop()
resolver = aiodns.DNSResolver(loop=loop)
loop.run_until_complete(fetch())可能有必要将解决方案作为可选特性包含到TCPConnector中。
欢迎拉出请求!
https://stackoverflow.com/questions/34700311
复制相似问题