UPD: found out cx_Oracle also has Connection.cancel() method. May be things are not that bad
On Monday, January 10, 2022 at 12:52:49 PM UTC+3 Don Nillo 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/c086eeea-cc4c-41de-ac7c-7f90811c63a6n%40googlegroups.com.