python network_error ai_generated true

httpx.ConnectTimeout: Connection timed out after 5000ms

ID: python/starlette-http-connection-timeout

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active
3.11 active

Root Cause

Starlette 应用尝试连接外部服务时,网络请求超时

generic

中文

Starlette 应用尝试连接外部服务时,网络请求超时

Workarounds

  1. 85% success 增加超时时间并添加重试机制
    client = httpx.AsyncClient(timeout=10.0)
    for i in range(3):
        try:
            response = await client.get(url)
            break
        except httpx.ConnectTimeout:
            await asyncio.sleep(1)
  2. 90% success 检查网络连接和服务可用性
    ping example.com
    curl -v http://example.com

Dead Ends

Common approaches that don't work:

  1. 无限增加超时时间 60% fail

    掩盖了根本问题,且可能导致资源耗尽

  2. 忽略异常而不重试 80% fail

    请求失败可能导致数据不一致