【问题标题】:How can I fix ConnectTimeout exceptions from within FastAPI如何从 FastAPI 中修复 ConnectTimeout 异常
【发布时间】:2020-10-22 18:46:38
【问题描述】:

我希望创建一个接收请求、进行一些处理并将请求转发到另一个端点的服务器。我似乎遇到了更高并发的问题,我的client.post 导致httpx.ConnectTimeout 异常。

我还没有完全排除端点出现问题的可能性(我目前正在与他们合作调试任何可能出现在他们身上的东西),但我正在尝试找出是否有问题我的结局,或者如果有任何明显的低效率我可以改进。

我在 ECS 中运行它,目前在一个任务有 4 个 vCPU 的集群上。我正在使用泊坞窗图像uvicorn-gunicorn-fastapi(https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker)。目前所有默认设置减去绑定/端口/日志记录。这是一个最小的代码示例:

import httpx
from fastapi import FastAPI, Request, Response

app = FastAPI()

def process_request(path, request):
    #Process Request Here

def create_headers(path):
    #Create headers here

@app.get('/')
async def root(path: str, request: Request):
    endpoint = 'https://endpoint.com/'
    querystring = 'path=' + path
    data = process_request(request, path, request)
    headers = create_headers(request)
    async with httpx.AsyncClient() as client:
        await client.post(endpoint + "?" + querystring, data=data, headers=headers)
    return Response(status_code=200)

【问题讨论】:

    标签: python python-3.x fastapi httpx


    【解决方案1】:

    可能是因为httpx 没有给其他端点足够的时间来完成请求,所以另一端的服务器占用了太多时间并且连接只是超时了?

    如果是,您可以尝试禁用超时或增加限制(我建议过度禁用)。

    https://www.python-httpx.org/quickstart/#timeouts

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 2012-10-19
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多