Can't make timeout work #2281
Unanswered
tamis-laan
asked this question in
Q&A
Replies: 2 comments
-
|
The 20-second delay you're seeing might be DNS resolution, which isn't covered by Try this: import httpx
client = httpx.AsyncClient(
timeout=httpx.Timeout(
connect=2.0, # TCP connection timeout
read=2.0, # Response read timeout
write=2.0, # Request write timeout
pool=2.0 # Pool connection timeout
)
)But DNS resolution is separate! If the delay is from DNS, try: import socket
# Set global DNS timeout
socket.setdefaulttimeout(2.0)
# Or use a custom transport with limits
import httpx
limits = httpx.Limits(max_connections=10, max_keepalive_connections=5)
client = httpx.AsyncClient(limits=limits, timeout=2.0)Debugging tips:
import time
start = time.time()
try:
socket.create_connection(("hostname", 80), timeout=2)
except:
pass
print(f"TCP connect: {time.time()-start}s")What's the target URL? Could be the server itself being slow to respond. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
The 20-second delay you're seeing might be DNS resolution, which isn't covered by Try this: import httpx
client = httpx.AsyncClient(
timeout=httpx.Timeout(
connect=2.0, # TCP connection timeout
read=2.0, # Response read timeout
write=2.0, # Request write timeout
pool=2.0 # Pool connection timeout
)
)But DNS resolution is separate! If the delay is from DNS, try: import socket
# Set global DNS timeout
socket.setdefaulttimeout(2.0)
# Or use a custom transport with limits
import httpx
limits = httpx.Limits(max_connections=10, max_keepalive_connections=5)
client = httpx.AsyncClient(limits=limits, timeout=2.0)Debugging tips:
import time
start = time.time()
try:
socket.create_connection(("hostname", 80), timeout=2)
except:
pass
print(f"TCP connect: {time.time()-start}s")What's the target URL? Could be the server itself being slow to respond. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm doing a request with httpx that returns:
httpx.ConnectTimeout
I waits 20 seconds for this. And I want to make it shorter, i.e. 2 seconds for example so I do:
httpx.AsyncClient(timeout = httpx.Timeout(2.0, connect=2.0))
But still it takes 20 seconds, am I doing something wrong here?
Beta Was this translation helpful? Give feedback.
All reactions