Max*_*Max 10 python postgresql sqlalchemy digital-ocean fastapi
我有一个使用 FastAPI 和 SQLAlchemy 构建的 Web 应用程序,它在本地使用 Docker 运行正常,但在带有托管 Postgres DB 的 DigitalOcean 上,db 查询失败并出现错误:
(psycopg2.OperationalError) 服务器意外关闭了连接\n\t这可能意味着服务器在处理请求之前或处理请求时异常终止\n\t。\n\n(此错误的背景:http ://sqlalche.me/e /14/e3q8 )"}
我之前在使用 Flask 时遇到过这个错误,问题是我必须设置引擎选项pool_pre_ping=True并将我的 cluster/droplet IP 添加到数据库的可信来源。但是看起来对于 FastAPI 这还不够。我还能做些什么来成功执行查询?
背景
pool_pre_ping=True设置
True在使用请求之前设置为正确session.get_bind().pool._pre_ping,它实际上是Trueuvicorn.workers.UvicornH11Worker工人使用 gunicorn 运行该应用程序class DBMiddleware:
def __init__(self, app, sqlalchemy_uri):
self.app = app
self.sqlalchemy_uri = sqlalchemy_uri
self.engine = None
async def __call__(self, scope: Scope, receive: Receive, send: Send):
if scope['type'] not in ['http', 'websocket']:
await self.app(scope, receive, send)
return
if not self.engine:
self.engine = create_engine(self.sqlalchemy_uri, pool_pre_ping=True, pool_recycle=3600)
session = Session(autoflush=False, autocommit=False, bind=self.engine)
scope['db'] = session
await self.app(scope, receive, send)
session.close()
def get_db(request: Request):
return request.scope.get('db')
...
@app.on_event('startup')
async def startup():
...
app.add_middleware(DBMiddleware, sqlalchemy_uri=config.SQLALCHEMY_DATABASE_URI)
@router.post('/endpoint')
async def endpoint(db: Session = Depends(get_db)):
...
Run Code Online (Sandbox Code Playgroud)
db.execute('SELECT 1')万一出现一些奇怪的超时或其他事情 - 仍然相同毕竟尝试了问题仍然存在。我不太了解asyncPython,所以我怀疑问题可能出在连接的共享方式或其他方面(但我目前只使用一名工人)。
更新
我试图切换到asyncpg(文档:https : //docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html)。也可以在本地运行,但在 DigitalOcean 上查询挂起,我收到以下错误:
[Errno 104] Connection reset by peer
Run Code Online (Sandbox Code Playgroud)
看起来原因是一样的,但是 asyncpg 的错误看起来不同。
还尝试在 DigitalOcean 上创建一个连接池并连接到它 - 仍然是同样的错误。
Lif*_*lex 11
您是否尝试将任何connect_args添加到sqlalchemy create_engine中?这些参数应该允许您维持与数据库的连接。
以下是可能有用的各种libpq 连接参数的列表。
create_engine(self.sqlalchemy_uri,
pool_pre_ping=True,
pool_recycle=3600, # this line might not be needed
connect_args={
"keepalives": 1,
"keepalives_idle": 30,
"keepalives_interval": 10,
"keepalives_count": 5,
}
)
Run Code Online (Sandbox Code Playgroud)
值得注意的是,任何使用psycopg的人也可以使用这些libpq 连接参数。
小智 1
这可能是一个有点笼统的答案,但以下步骤将有助于定位问题:
| 归档时间: |
|
| 查看次数: |
646 次 |
| 最近记录: |