On Mon, Nov 12, 2018 at 6:12 PM Brian Cherinka <
havo...@gmail.com> wrote:
>
> Hmm. Ok. I'm using a postgres database with the psycopg2 driver. I'm aware of the `statement_timeout` option in `postgres` which I can pass into the psycopg2 `connect` method. As far as I can tell there's not a way to pass that keyword in through SQLAlchemy after the db engine has been created. Is that correct? Does the `query.execution_options`, or something in session, accept that keyword?
if statement_timeout is accepted only on the "connect" method and not
once you have already connected, then it would need to be set for the
Engine globally. You would do this using the "connect" event:
https://docs.sqlalchemy.org/en/latest/core/events.html?highlight=connect%20event#sqlalchemy.events.PoolEvents.connect
Otherwise, if it can be set on a cursor, you can use
execution_options(), which you intercept with a before_cursor_execute
event:
@event.listens_for(Engine, "before_cursor_execute")
def _set_timeout(conn, cursor, stmt, params, context, executemany):
timeout = conn._execution_options.get('timeout', None)
if timeout:
cursor.statement_timeout = timeout
query.execution_options() accepts whatever keywords you pass into it
and you can get them out inside that event handler (or anywhere you
have a Connection).