'SQLAlchemy: Is there an event that fires after session.execute runs but before results are returned?

I have an python/SQLAlchemy application in AWS lambda. This application starts a LOAD DATA query on a MySQL db. Because this query is very long-running (longer than the 15-minute cutoff for lambda) it is designed to run to completion even in the case of a disconnect (it won't roll back).

The idea is to run this query in a thread and kill that thread as soon as MySQL receives the query.

I am looking at SQLAlchemy events but none of them seem to do what I want. I have tried the following ones:

@event.listens_for(db.engine, 'after_cursor_execute')
def receive_after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
    print("after_cursor_execute")


@event.listens_for(db.engine, "after_execute")
def receive_do_orm_execute(conn, clauseelement, multiparams, params, execution_options, result):
    print("after_execute")


@event.listens_for(db.session, 'after_flush')
def receive_after_flush(session, flush_context):
    print('after_flush')


@event.listens_for(db.session, 'after_flush_postexec')
def receive_after_flush_pe(session, flush_context):
    print('after_flush_postexec')


@event.listens_for(db.session, 'after_transaction_create')
def receive_after_transaction_create(session, transaction):
    print('after_transaction_create')


@event.listens_for(db.session, 'after_begin')
def receive_after_begin(session, transaction, connection):
    print("after_begin")

Is there an event that would do what I want? I can execute through the session or engine, so that's not a limitation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source