0

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

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

我在 ECS 中运行它,目前在一个任务有 4 个 vCPU 的集群上。我正在使用 docker 镜像uvicorn-gunicorn-fastapihttps://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)
4

1 回答 1

-1

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

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

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

于 2020-11-23T08:05:38.060 回答