I'm not sure this is even possible. Ignoring SQLAlchemy, do you know how you would do it using any MySQL DBAPI library (mysqlclient, PyMySQL, etc.)?
Maybe you could do something creative where you capture the PID when the connection is created, and on Ctrl-C, send a "KILL pid" command. I don't know if you can send new commands on a connection while it is executing a query, so you might need to use a new connection. Simon On Thu, Nov 14, 2019 at 8:24 AM Marcin Koziej <mar...@cahoots.pl> wrote: > > (Crossposting from Stackoverflow, where I unfortunately didn't get any > answers: > https://stackoverflow.com/questions/58837864/sqlalchemty-how-to-kill-a-mysql-process-on-keyboard-interrupt > ) > > Hello! > > I'm using SQLAlchemy 1.3.10 to run a bunch of SQL statements on Percona > Server 5.7.27. I do not use the ORM functionality. My problem is that I > cannot abort the running SQL statement from KeyboardInterrupt handler, after > hitting Ctrl-C. > > My usage is as follows: > > engine = create_engine(...) > connection = engine.connect() > with connection.begin() as transaction: > try: > res = connection.execute(text(sql)) > except KeyboardInterrupt as ctrlc: > print('You pressed ctrl-c') > transaction.rollback() > sys.exit(1) > > > Pressing Ctrl-c stops the script but leaves the query running in MySQL > server, and i have to kill it manually using KILL pid. > > I've tried to call connection.close() as well, but without result. How can I > kill such running query on keyboard interrupt? > > > Best regards, > > > Marcin Koziej > > -- > 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/426f7140-d078-41fa-81d7-a5a6aabb2a7f%40googlegroups.com. -- 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/CAFHwexewtrjbv%2BYLcDgx0y1o0Sg3o6qO56Oyxf7OEvUyprzRJQ%40mail.gmail.com.