相关疑难解决方法(0)

如何在指定时间后停止执行 FastAPI 端点以减少 CPU 资源使用/成本?

使用案例

调用 的客户端微服务/do_something在 request/post() 调用中超时为 60 秒。此超时是固定的且无法更改。因此,如果/do_something需要 10 分钟,/do_something就会浪费 CPU 资源,因为客户端微服务在 60 秒后不会等待 的响应/do_something,这会浪费 CPU 10 分钟,从而增加成本。我们的预算有限。

当前的代码如下所示:

import time
from uvicorn import Server, Config
from random import randrange
from fastapi import FastAPI

app = FastAPI()

def some_func(text):
    """
    Some computationally heavy function
    whose execution time depends on input text size
    """
    randinteger = randrange(1,120)
    time.sleep(randinteger)# simulate processing of text
    return text


@app.get("/do_something")
async def do_something():
    response = some_func(text="hello world")
    return {"response": …
Run Code Online (Sandbox Code Playgroud)

python timeout fastapi

4
推荐指数
1
解决办法
6784
查看次数

标签 统计

fastapi ×1

python ×1

timeout ×1