[英]Is there a way to stop the execution of a mysql query in python if a python exception occurs?
I'm running a python script that executes mysql queries with many rows. 我正在运行一个python脚本,该脚本执行具有许多行的mysql查询。 The query takes a lot of time to run and sometimes I need to stop the script to change something or to start over. 该查询需要大量时间才能运行,有时我需要停止脚本以更改某些内容或重新开始。 Is there a way to tell the mysql server that the script is terminating (for example if a KeyboardInterrupt exception occurs) and that the result of the query is no longer needed? 有没有办法告诉mysql服务器脚本正在终止(例如,如果发生KeyboardInterrupt异常)并且不再需要查询结果?
I tried closing the mysql connection and it didn't work. 我尝试关闭mysql连接,但没有成功。 The execution of the query keeps on going. 查询的执行一直在进行。
I know that I can kill the query from the server directly but I would like the script to do it by itself. 我知道我可以直接终止服务器上的查询,但是我希望脚本自己完成。
I'm using the mysql-connector-python library, version is 2.0.2. 我正在使用mysql-connector-python库,版本是2.0.2。
You can wrap you code in try except block and kill the query once exception raised. 您可以将代码包装在try例外中,并在引发异常后终止查询。 Something like that: 像这样:
pid = <get the pid of the query>
try:
.... your code
except:
cursor.execute('kill %s', pid)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.