Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set connection timeout in SQLAlchemy

I'm trying to figure out how to set the connection timeout in create_engine(), so far I've tried:

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) 

What should I do?

like image 828
daveoncode Avatar asked Feb 25 '16 23:02

daveoncode


People also ask

Does SQLAlchemy close connection automatically?

close() method is automatically invoked at the end of the block. The Connection , is a proxy object for an actual DBAPI connection. The DBAPI connection is retrieved from the connection pool at the point at which Connection is created.

Does SQLAlchemy use connection pool?

SQLAlchemy includes several connection pool implementations which integrate with the Engine . They can also be used directly for applications that want to add pooling to an otherwise plain DBAPI approach.

How do I close a SQLAlchemy connection?

you call close(), as documented. dispose() is not needed and in fact calling dispose() explicitly is virtually never needed for normal SQLAlchemy usage.

What is connection timeout?

Connection timeout is on the client's side, usually meaning that the client lost connection, or is unable to establish connection to a server for whatever reason (such as remote firewall is dropping the traffic or the server went down).


1 Answers

The right way is this one (connect_timeout instead of connection_timeout):

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

...and it works with both Postgres and MySQL

ps: (the timeout is defined in seconds)

like image 120
daveoncode Avatar answered Sep 28 '22 08:09

daveoncode