On Thu, Nov 14, 2019, at 3:24 AM, Marcin Koziej 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)
`


that's the driver you're using which would not be allowing keyboard interrupt 
to go through.

I just tried this with mysqlclient and pymysql and I would assume you're using 
mysqlclient as it seems to block on keyboard interrupt:

>>> e = create_engine("mysql+mysqldb://scott:tiger@localhost/test")
>>> e.execute("select sleep(10)")
pressed ctrl c, 
10 seconds later...
... KeyboardInterrupt

e.g. it raised, but it made me wait the whole time.

Pymysql, which is pure Python, no problem:

>>> e = create_engine("mysql+pymysql://scott:tiger@localhost/test")
>>> e.execute("select sleep(10)")
pressed ctrl c,
immediate KeyboardInterrupt





> 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?




ah OK well that part, once you're back in control like Simon mentioned you want 
to search though the processlist and then use kill: 
https://dev.mysql.com/doc/refman/8.0/en/kill.html


although I wonder if there is some option on mysqlclient to change this. 
mysqlclient's author doesn't have a lot of time to respond to questions but you 
can try asking at https://github.com/PyMySQL/mysqlclient-python (same 
maintainer for both projects).

I would try out pymysql and see if there is any difference in behavior.



> 

> 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
>  
> <https://groups.google.com/d/msgid/sqlalchemy/426f7140-d078-41fa-81d7-a5a6aabb2a7f%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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/7be0bd76-cb5e-4e7e-92a5-70a35063e7bd%40www.fastmail.com.

Reply via email to