[Python] How do I detect a database timeout in python?

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have some code that executes database queries, like so:

    self.db_cursor = self.db_conn.cursor(buffered=False)
    self.db_cursor.execute(query)


    Now I want to add a timeout, so that long queries are killed. I can do this in MYSQL like this:

    self.db_conn.reset_session(session_variables={'max_execution_time': 10})


    (I deliberately set the timeout to be crazy short, for testing.)

    How can I tell if a query timed out? I want to be able to report back to the user. There's no exception thrown, no warnings on the cursor, I can't find anything to check against.

    Continue reading...

Compartilhe esta Página