I use pymysql
to connect mysql and I set a read_timeout
when connecting:
conn = pymysql.connect(host=host,port=port,user=user,passwd=passwd,read_timeout=60)
and my query code is here:
try:
cur.execute(sql)
rows = cur.fetchall()
except:
# timeout
logging.error()
finally:
cur.close()
conn.close()
When I execute a long time query, the code raise a timeout error and finally cursor
and connection
are both closed(I debug conn._closed
to confirm the connection is closed).But when I login to mysql and show processlist
, I found the process is still alive.
My question is, is this normal and How can I kill the process when query timeout in my code? I figure out a workaround that execute set session max_execution_time=60
before query, but I think it's not the best practice.
Here is show processlist
output, I use sleep
to represent long time query, I think the result is the same, connection
is closed in my code, but process is still there.