并发HTTP GET请求是指同时发送多个HTTP GET请求以提高效率和处理能力的技术。在现代网络应用中,这已成为提高性能的关键手段。
import threading
import requests
def fetch_url(url):
response = requests.get(url)
print(f"{url}: {len(response.content)} bytes")
urls = ["https://example.com/page1", "https://example.com/page2"]
threads = []
for url in urls:
thread = threading.Thread(target=fetch_url, args=(url,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
import asyncio
import aiohttp
async def fetch_url(session, url):
async with session.get(url) as response:
content = await response.read()
print(f"{url}: {len(content)} bytes")
async def main():
urls = ["https://example.com/page1", "https://example.com/page2"]
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
await asyncio.gather(*tasks)
asyncio.run(main())
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
session = requests.Session()
retries = Retry(total=5, backoff_factor=1)
session.mount('https://', HTTPAdapter(max_connections=100, max_retries=retries))
urls = ["https://example.com/page1", "https://example.com/page2"]
responses = [session.get(url) for url in urls]
for response in responses:
print(f"{response.url}: {len(response.content)} bytes")
原因:并发数过高导致服务器过载或被防火墙拦截 解决方案:
原因:同时保存大量响应对象 解决方案:
原因:网络问题或服务器不稳定 解决方案:
# 带有重试和超时的示例
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
session = requests.Session()
retry = Retry(
total=3,
backoff_factor=0.3,
status_forcelist=[500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.get('https://example.com', timeout=5)
并发HTTP GET请求技术将继续演进,以适应日益增长的互联网应用需求,同时保持高效、稳定和安全。