I don't know how to do this using other DBAPIs, but I thought that if command 
line mysql client has such behaviour, it should be possible also using the 
library.
I was thinking about finding the process in SHOW PROCESSLIST and killing it, 
but it seems hacky and I am not sure if I can find the correct process (what if 
the same user is running another query concurrently?). Does SQLAlchemy store 
last command pid maybe?


-- 
Marcin Koziej
GPG key: https://go.cahoots.pl/gpg/  Ϟ  Twitter: @movonw

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Thursday, November 14, 2019 12:37 PM, Simon King <si...@simonking.org.uk> 
wrote:

> 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 a topic in the Google 
> Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/sqlalchemy/Wv6xGeBLrjg/unsubscribe.
> To unsubscribe from this group and all its topics, 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.


-- 
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/YVEyBt4_-K_PvH4mN4Kc8d-CQyxe85bUy_6kXx668ub5FvPkR82zrxfkYlTHHW2ptapq1z-QRoX9fGqWzGvmkt9RqusI6Ow65cQTZO4x4EA%3D%40cahoots.pl.

Reply via email to