gpt4 book ai didi

python - 使用 aiohttp 的 HEAD 请求很慢

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:56 25 4
gpt4 key购买 nike

给定一个包含 50k 个网站 url 的列表,我的任务是找出其中哪些是可用的/可访问的。这个想法只是向每个 URL 发送一个 HEAD 请求并查看状态响应。据我所知,异步方法是可行的方法,目前我正在使用 asyncioaiohttp

我想出了以下代码,但速度非常糟糕。在我的 10 兆位连接上,1000 个 URL 大约需要 200 秒。我不知道期望的速度是多少,但我是 Python 异步编程的新手,所以我认为我在某个地方出错了。如您所见,我已经尝试将允许的同时连接数增加到 1000(从默认值 100 增加)以及 DNS 解析在缓存中保留的持续时间;都没有什么大的影响。环境有Python 3.6和aiohttp 3.5.4。

与问题无关的代码审查也很受欢迎。

import asyncio
import time
from socket import gaierror
from typing import List, Tuple

import aiohttp
from aiohttp.client_exceptions import TooManyRedirects

# Using a non-default user-agent seems to avoid lots of 403 (Forbidden) errors
HEADERS = {
'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/45.0.2454.101 Safari/537.36'),
}


async def get_status_code(session: aiohttp.ClientSession, url: str) -> Tuple[int, str]:
try:
# A HEAD request is quicker than a GET request
resp = await session.head(url, allow_redirects=True, ssl=False, headers=HEADERS)
async with resp:
status = resp.status
reason = resp.reason
if status == 405:
# HEAD request not allowed, fall back on GET
resp = await session.get(
url, allow_redirects=True, ssl=False, headers=HEADERS)
async with resp:
status = resp.status
reason = resp.reason
return (status, reason)
except aiohttp.InvalidURL as e:
return (900, str(e))
except aiohttp.ClientConnectorError:
return (901, "Unreachable")
except gaierror as e:
return (902, str(e))
except aiohttp.ServerDisconnectedError as e:
return (903, str(e))
except aiohttp.ClientOSError as e:
return (904, str(e))
except TooManyRedirects as e:
return (905, str(e))
except aiohttp.ClientResponseError as e:
return (906, str(e))
except aiohttp.ServerTimeoutError:
return (907, "Connection timeout")
except asyncio.TimeoutError:
return (908, "Connection timeout")


async def get_status_codes(loop: asyncio.events.AbstractEventLoop, urls: List[str],
timeout: int) -> List[Tuple[int, str]]:
conn = aiohttp.TCPConnector(limit=1000, ttl_dns_cache=300)
client_timeout = aiohttp.ClientTimeout(connect=timeout)
async with aiohttp.ClientSession(
loop=loop, timeout=client_timeout, connector=conn) as session:
codes = await asyncio.gather(*(get_status_code(session, url) for url in urls))
return codes


def poll_urls(urls: List[str], timeout=20) -> List[Tuple[int, str]]:
"""
:param timeout: in seconds
"""
print("Started polling")
time1 = time.time()
loop = asyncio.get_event_loop()
codes = loop.run_until_complete(get_status_codes(loop, urls, timeout))
time2 = time.time()
dt = time2 - time1
print(f"Polled {len(urls)} websites in {dt:.1f} seconds "
f"at {len(urls)/dt:.3f} URLs/sec")
return codes

最佳答案

现在您正在同时启动所有请求。因此可能在某处出现了瓶颈。避免这种情况semaphore可以使用:

# code

sem = asyncio.Semaphore(200)


async def get_status_code(session: aiohttp.ClientSession, url: str) -> Tuple[int, str]:
try:
async with sem:
resp = await session.head(url, allow_redirects=True, ssl=False, headers=HEADERS)
# code

我按照以下方式测试了它:

poll_urls([
'http://httpbin.org/delay/1'
for _
in range(2000)
])

得到:

Started polling
Polled 2000 websites in 13.2 seconds at 151.300 URLs/sec

虽然它请求单个主机,但它表明异步方法可以完成工作:13 秒。 < 2000 秒

还有几件事可以做:

  • 你应该发挥信号量的值(value)以获得更好的性能针对您的具体环境和任务。

  • 尝试将超时从 20 降低到比方说 5seconds:因为你只是在做 head request 所以应该不会花太多时间时间。如果请求挂起 5 秒,它很可能不会挂起完全成功。

  • 在脚本运行时监控您的系统资源(网络/CPU/RAM)可以帮助查明瓶颈是否仍然存在。

  • 顺便问一下,您是否安装了 aiodns(如 doc 所建议的那样)?

  • 是否disabling ssl改变了什么?

  • 尝试启用 logging 的调试级别看看那里有没有什么有用的信息

  • 尝试设置 client tracing尤其是测量每个请求步骤的时间,看看哪些步骤花费的时间最多

如果没有完全可重现的情况,很难说更多。

关于python - 使用 aiohttp 的 HEAD 请求很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com