Skip to content

Instantly share code, notes, and snippets.

@zmsmith
Created July 3, 2012 01:45
Show Gist options
  • Save zmsmith/3037007 to your computer and use it in GitHub Desktop.
Save zmsmith/3037007 to your computer and use it in GitHub Desktop.
Django - Kill Queries Matching a RegEx
def kill_queries(query='', db_alias='default'):
from django.db import connections
import re
connection = connections[db_alias]
cursor = connection.cursor()
cursor.execute("SHOW PROCESSLIST")
for row in cursor.fetchall():
current = row[-1]
if current and re.search(query, current):
proc_id = row[0]
kill = "KILL {}".format(proc_id)
try:
cursor.execute(kill)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment