SQLAlchemy supports connection timeouts to establish a connection already.

SQLAlchemy does not, and can not, support query timeouts.  This is possible 
with some python database drivers, but very rare.  In every Python database 
program/library query timeouts are typically handled on the database 
server, and almost never on Python.  You can use SQLAlchemy's engine events 
to emit sql that will set/clear a timeout on the database server.

This has come up many times in the past, and there are several threads in 
the group history that explain the details and offer solutions.

On Monday, January 10, 2022 at 4:52:49 AM UTC-5 donnill...@gmail.com wrote:

> Hi!
> Sorry for bothering, I have not enough skills to contribute yet (
> But...
> I think it would be great to have some feature to easily set connection 
> timeouts in SQLAlchemy 2.0.
> The reason is I faced a problem where I was unable to cancel 
> some erroneous time-consuming queries with SQLAlchemy Core. And I guess, I 
> am not the only one.
> My straightforward nooby solution so far is this:
>
> from threading import Timer
>
> with engine.connect() as connection:
>   timeout = Timer(MAX_EXECUTION_TIME, lambda: 
> connection.connection.connection.cancel())
>   timeout.start()
>   r = connection.execute(stmt).freeze() # I just love FrozenResult)
>   timeout.cancel()
>
> The bad thing this is dialect-specific and works only due to cancel() 
> method in psycopg2
> I was also trying to benefit from handling sqlalchemy.events but failed...
> One of my intentions was to modify before_execute() method to catch 
> unintended cartesian product queries and raise error instead of throwing 
> warning.
> Unfortunately, at the moment this feels like too low-level for me.
>
> What I wish to have is something like this:
>
> with engine.connect(timeout=MAX_EXECUTION_TIME) as connection:
>   r = connection.execute(stmt)
>
> I hope somebody smart enough could seize time to think about it.
> This would make me happy. 
>
> Thanks in advance!
>
>
>
> [image: thanks-in-advance.png]
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/82b228ef-338f-4876-9cc4-4eb998dc04fan%40googlegroups.com.

Reply via email to