python - 如何在 SQLAlchemy 中设置连接超时

标签 python postgresql sqlalchemy psycopg2

我正在尝试弄清楚如何在 create_engine() 中设置连接超时,到目前为止我已经尝试过:

create_engine(url, timeout=10)

TypeError: Invalid argument(s) 'timeout' sent to create_engine(), using configuration PGDialect_psycopg2/QueuePool/Engine. Please check that the keyword arguments are appropriate for this combination of components.

create_engine(url, connection_timeout=10)

TypeError: Invalid argument(s) 'connection_timeout' sent to create_engine(), using configuration PGDialect_psycopg2/QueuePool/Engine. Please check that the keyword arguments are appropriate for this combination of components.

create_engine(db_url, connect_args={'timeout': 10})

(psycopg2.OperationalError) invalid connection option "timeout"

create_engine(db_url, connect_args={'connection_timeout': 10})

(psycopg2.OperationalError) invalid connection option "connection_timeout"

create_engine(url, pool_timeout=10)

我该怎么办?

最佳答案

正确的方式是这个(connect_timeout而不是connection_timeout):

create_engine(db_url, connect_args={'connect_timeout': 10})

...它适用于 Postgres 和 MySQL

docs sqlalchemy connect-args

ps:(超时以秒为单位)

关于python - 如何在 SQLAlchemy 中设置连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640726/

相关文章:

postgresql - USING 部分在 EXECUTE 中被忽略...在 PL/pgSQL 中使用

windows - 找不到角色 "username": PostgreSQL Windows

python - 如何使用函数在 sqlalchemy 查询中验证密码?

python - 如何计算列表的最小不公平总和

python 列表 : double each element in a list while maintaining the structure

sql - 除法结果为零时返回字符串

python - 如何按连接数对查询进行排序

python - 让 namedtuple 接受 kwargs

python - 来自 cookie 的 Python MediaWiki 身份验证

python - 如何将图像添加到模型(表)并将其保存到 FastAPI 框架中的数据库?