FastAPI 和 Python 个线程

FastAPI and Python threads

我对 FastAPI 和额外的线程生成有一点疑问。假设我有一个服务于两个端点的应用程序。

  1. 其中之一 /create_user 在某些数据库中创建用户
  2. 其他只是/ping。 Ping 是因为我的应用程序在 Kubernetes 中 运行 并且它通过发送 GET 请求并接收 response_code 200.
  3. 不断检查我的应用程序是否存在
  4. 此外,我还有一个使用 threading.Thread 制作的单独进程,它从外部服务接收一些密钥。 key有TTL,需要时常更新

问题是当我通过第一个端点加载数据时,我正在加载的数据库非常慢,最多可以回答 10 秒。在那一刻,所有其他端点(包括 /ping)都被锁定。所以 k8s 认为我的应用程序已死并尝试回滚它。

我可以简单地尝试增加使用命令 uvicorn main:app --workers 4 为应用程序服务的工人数量 但是每个工作人员也产生了额外的线程,日志中的输出看起来像那样

INFO:     Application startup complete.
Hello from additional thread
INFO:     Started server process [88030]
Hello from additional thread 
INFO:     Waiting for application startup
Hello from additional thread Hello from additional thread
INFO:     Application startup complete. Hello from additional thread

我的问题是否可以使用多个 gunicorn worker 只产生一个额外的线程?

这是我的 main.py

的代码片段
@app.post("/api/v1/create_user")
async def create_user() -> JSONResponse:
    """Some creation magic here"""
    return JSONResponse(status_code=status.HTTP_201_CREATED, content={"Success": True,                                                                     "Username":raw_credentials["user"]})
    
    
@app.get("/ping", status_code=status.HTTP_200_OK)
async def dummy_response():
    return

# Special treads lunching for some jobs that need to be repeated during app lifecycle.
t1 = Thread(target=renew_api_token)
t1.start()

我认为主要问题是您可能没有使用异步兼容库来访问您的数据库。

这就是您在应用程序等待数据库时看到所有其他端点被锁定的原因。

这个问题有两个解决方案。

您可以找到一个异步库来访问您的数据库。

或者您可以使用 def create_user() 而不是 async def create_user(),这样 FastAPI 将 运行 在线程池中为您运行。