python aiohttp 超时是针对单个 TCP 连接还是针对 http 请求?
python aiohttp timeout is for a single TCP connection or for a http request?
在python aiohttp 中,我们可以在ClientSession
或(例如)session.get
中设置超时。 https://docs.aiohttp.org/en/stable/client_quickstart.html
假设我们这样做
async with aiohttp.ClientSession(timeout=<customized timeout>) as session:
async with session.get(<url1>):
xxx
async with session.get(<url2>):
xxx
customized timeout
是整个 async with aiohttp.ClientSession()
还是每个 async with session.get
?
超时是针对每个单独的请求;你可以这样测试它:
import asyncio
import aiohttp
import random
async def main():
timeout = aiohttp.ClientTimeout(total=3)
async with aiohttp.ClientSession(timeout=timeout) as session:
base_url = "http://httpbin.org/delay/%d"
for i in range(10):
url = base_url % random.randint(1, 5)
try:
async with session.get(url) as response:
data = await response.json()
print(f"step {i}, url {url}, status {response.status}, keys: {len(data)}")
except asyncio.exceptions.TimeoutError:
print(f"step {i}, url {url}, status timeout")
asyncio.run(main())
我电脑上的结果:
step 0, url http://httpbin.org/delay/5, status timeout
step 1, url http://httpbin.org/delay/5, status timeout
step 2, url http://httpbin.org/delay/2, status 200, keys: 7
step 3, url http://httpbin.org/delay/4, status timeout
step 4, url http://httpbin.org/delay/4, status timeout
step 5, url http://httpbin.org/delay/3, status 200, keys: 7
step 6, url http://httpbin.org/delay/2, status 200, keys: 7
step 7, url http://httpbin.org/delay/3, status 200, keys: 7
step 8, url http://httpbin.org/delay/1, status 200, keys: 7
step 9, url http://httpbin.org/delay/2, status 200, keys: 7
在python aiohttp 中,我们可以在ClientSession
或(例如)session.get
中设置超时。 https://docs.aiohttp.org/en/stable/client_quickstart.html
假设我们这样做
async with aiohttp.ClientSession(timeout=<customized timeout>) as session:
async with session.get(<url1>):
xxx
async with session.get(<url2>):
xxx
customized timeout
是整个 async with aiohttp.ClientSession()
还是每个 async with session.get
?
超时是针对每个单独的请求;你可以这样测试它:
import asyncio
import aiohttp
import random
async def main():
timeout = aiohttp.ClientTimeout(total=3)
async with aiohttp.ClientSession(timeout=timeout) as session:
base_url = "http://httpbin.org/delay/%d"
for i in range(10):
url = base_url % random.randint(1, 5)
try:
async with session.get(url) as response:
data = await response.json()
print(f"step {i}, url {url}, status {response.status}, keys: {len(data)}")
except asyncio.exceptions.TimeoutError:
print(f"step {i}, url {url}, status timeout")
asyncio.run(main())
我电脑上的结果:
step 0, url http://httpbin.org/delay/5, status timeout
step 1, url http://httpbin.org/delay/5, status timeout
step 2, url http://httpbin.org/delay/2, status 200, keys: 7
step 3, url http://httpbin.org/delay/4, status timeout
step 4, url http://httpbin.org/delay/4, status timeout
step 5, url http://httpbin.org/delay/3, status 200, keys: 7
step 6, url http://httpbin.org/delay/2, status 200, keys: 7
step 7, url http://httpbin.org/delay/3, status 200, keys: 7
step 8, url http://httpbin.org/delay/1, status 200, keys: 7
step 9, url http://httpbin.org/delay/2, status 200, keys: 7