调用 的客户端微服务/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)