FastAPI 数据库查询卡住。库伯内斯

FastAPI db query stuck. Kubernetes

我有一个使用 fastapi 的小应用程序。问题是当我将它部署到我的服务器并尝试向包含一些数据库操作的路由发出 post 请求时,它只是卡住并给我 504 错误。但在我的本地机器上运行良好。

这是我的数据库连接方式:

app.add_event_handler("startup", tasks.create_start_app_handler(app))
app.add_event_handler("shutdown", tasks.create_stop_app_handler(app))

我试图将数据库连接从启动应用程序恢复到创建此连接,并在不同的路径中关闭它以进行测试,并且它的工作正常。喜欢:

@app.get("/")
async def create_item():
    engine = create_engine(
        DB_URL
    )
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

    Base = declarative_base()

    t = engine.execute('SELECT * FROM auth_user').fetchone()

    engine.dispose()
    return t

它如何取决于事件? postgresql的版本不同,但我认为不是因为它。

目前我已经部署了 2 pods 运行。当我使用 psql 命令时,我可以正常连接。所以它只卡在应用程序中,而不是 pod。

如果有人发现相同,我通过将 pgpoll 从 4.2.2 更新到最新版本来修复它。