python - FastAPI 和 Python 线程

标签 python multithreading python-3.7 python-multithreading fastapi

我对 FastAPI 和其他线程产生了一点问题。假设我有一个服务于两个端点的应用程序。

  1. 其中一个要 /create_user 在某个数据库中创建用户
  2. 其他只是 /ping。 Ping 只是因为我的应用在 Kubernetes 中运行,它通过发送 GET 请求并接收 response_code 200 来不断检查我的应用是否处于事件状态。
  3. 另外,我有一个使用 threading.Thread 制作的单独进程,它从外部服务接收一些 key 。 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 将在 ThreadPool 中为您运行该函数。

关于python - FastAPI 和 Python 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64221720/

相关文章:

Python Queue.get(block=False) 与非空队列。它可以引发Queue.empty吗?

python - 使用有序字典作为有序集

python - 确定哪个版本的 OpenCV

python - pandas groupby 两列并从第二列生成列

python - 在 virtualenvwrapper 环境中安装 GDAL

python - 如何使用opencv python将RGB颜色范围更改为红色

python - 如何只打印字典中的一项

python - 使用 scipy 拟合给定直方图的分布

java - 将服务层传递给线程的正确方法

java - 并发 - 中断 Future 而不取消它