Python sqlite3:如何使用例如KeyboardInterrupt快速干净地中断长时间运行的查询

时间:2017-04-04 17:20:11

标签: sqlite python

使用Python中的sqlite3模块,当收到SIGINT(例如Control-C)时,不会快速中断/取消长时间运行的查询。 sqlite3提供了interrupt()方法,但没有关于如何使用它的示例。

是否有一种简单的方法可以中断/取消通过Python / sqlite3运行的长时间查询?

为了说明,首先生成一个测试数据库&表:

import sqlite3
from random import randint

conn = sqlite3.connect("randtable.db", 10.0)
cursor = conn.cursor()

cursor.execute("CREATE TABLE randint (id integer, rand integer)")

for i in range(1000000):
    if i % 1000 == 0:
        print ("{0}...".format(i))
    rand = randint(0,1000000)
    cursor.execute("INSERT INTO randint VALUES ({0},{1})".format(i,rand))

conn.commit()
conn.close()

然后在终端中执行长时间运行的Python / sqlite3脚本并尝试使用Control-C中断它:

from __future__ import print_function
import sqlite3

def main():
    # Long running query (pathological by design)
    statement ='''
SELECT DISTINCT a.id,a.rand
FROM randint a
     JOIN randint b ON a.id=b.rand
     JOIN randint c ON a.id=c.rand
     JOIN randint d ON a.id=d.rand
     JOIN randint e ON a.id=e.rand
     JOIN randint f ON a.id=f.rand
     JOIN randint g ON a.id=g.rand
     JOIN randint h ON a.id=h.rand
ORDER BY a.id limit 10'''

    conn = sqlite3.connect('randtable.sqlite', 10.0)
    cursor = conn.cursor()

    print ("Executing query")

    cursor.execute(statement)
    rows = cursor.fetchall()

    print ("ROWS:")
    for row in rows:
        print ("  ", row)

    conn.close()

    return

if __name__ == "__main__":
    main()

在终端中运行上述脚本,然后按Control-C(或以其他方式发送SIGINT)将最终取消查询和脚本,但它可能需要相当多的时间,很多分钟。当按下Control-C时,sqlite3 command line tool中运行的完全相同的查询几乎立即取消。

提前致谢!

2 个答案:

答案 0 :(得分:2)

你的答案涵盖了它,但是(昨天让我忘记了 - 抱歉!)我记得我答应写一个答案,所以这里有另一个版本证明你可以在没有全局的情况下做到这一点。我还在这里使用了threading.Event而不是信号来证明有一些不同的方式来表示线程是时候做某事(但为了你的目的,坚持下去)信号,因为它非常适合对Ctrl + C作出反应):

import sqlite3
import time
import threading

# Background thread that'll kill our long running query after 1 second
def kill_it(connection, event):
    event.wait()
    time.sleep(1)
    connection.interrupt()

# Make some tables with lots of data so we can make a long running query
def big_query(conn, kill_event):
    print('Making big tables')
    conn.execute(
        "CREATE TABLE foo (i integer primary key, s text);")
    conn.execute(
        "CREATE TABLE bar (j integer primary key, s text);")
    conn.execute(
        "INSERT INTO foo VALUES %s" % ", ".join("(%d, 'foo')" % i for i in range(10000)))
    conn.execute(
        "INSERT INTO bar VALUES %s" % ", ".join("(%d, 'bar')" % i for i in range(10000)))
    kill_event.set()
    print('Running query')
    cur = conn.cursor()
    cur.execute(
        "SELECT * FROM foo, bar")
    print(len(cur.fetchall()))

def main():
    conn = sqlite3.connect('foo.db')
    kill_event = threading.Event()
    kill_thread = threading.Thread(target=kill_it, args=(conn, kill_event))
    kill_thread.start()
    big_query(conn, kill_event)
    kill_thread.join()

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

回答我自己的问题,因为我认为我已经解决了。以下是我提出的建议,对此代码的任何评论都将不胜感激。

#!/usr/bin/env python

from __future__ import print_function
import sqlite3
import threading
import signal
import os
import time

conn = None
shutdown = False

def main():
    global conn

    # Long running query (pathological by design)
    statement ='''
SELECT DISTINCT a.id,a.rand
FROM randint a
     JOIN randint b ON a.id=b.rand
     JOIN randint c ON a.id=c.rand
     JOIN randint d ON a.id=d.rand
     JOIN randint e ON a.id=e.rand
     JOIN randint f ON a.id=f.rand
     JOIN randint g ON a.id=g.rand
     JOIN randint h ON a.id=h.rand
ORDER BY a.id limit 10'''

    conn = sqlite3.connect('randtable.sqlite', 10.0)
    cursor = conn.cursor()

    print ("Executing query")

    try:
        cursor.execute(statement)
    except Exception as err:
        if str(err) != "interrupted":
            print ("Database error: {0}".format(str(err)))
        return None

    rows = cursor.fetchall()

    print ("ROWS:")
    for row in rows:
        print ("  ", row)

    conn.close()
    conn = None

    return

def interrupt(signum, frame):
    global conn
    global shutdown

    print ("Interrupt requested")

    if conn:
        conn.interrupt()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, interrupt)

    mainthread = threading.Thread(target=main)
    mainthread.start()

    while mainthread.isAlive():
        time.sleep(0.2)