I am new to python and would like to do what ADO .NET CommandTimeout property does (setting the execution time for a query) in mysql (5.6).
I am looking for any in-built python libraries that may support it by default (I have tried pymysql, pyodbc and mysql.connector) or what could the best way to do this, in case I have to invent the wheel from scratch.
I am working with an old mysql version (5.6), and the reason I am stressing on this is that on researching I have found out that this could be accomplished in mysql 5.7 onwards by using the following commands
SET SESSION MAX_EXECUTION_TIME=2000; SET GLOBAL MAX_EXECUTION_TIME=2000;
Reference Links I have already gone through which suggests a solution in mysql 5.7 and upwards: http://mysqlserverteam.com/server-side-select-statement-timeouts/ https://dev.mysql.com/doc/refman/5.7/en/optimizer-hints.html#optimizer-hints-execution-time
But I am looking for a solution in mysql 5.6, Thanks in advance.
Advertisement
Answer
So I was finally able to solve the issue, by creating a thread/process which is invoked based on a timer as follows:
query_monitoring_thread = threading.Timer(self.__command_timeout, self.kill_query, args=[mydb.connection_id, source])
The thread would get all the processes created by the current user and who is executing for time more than the command timeout and kill the ones that exist:
cursor.execute( "Select Id From Information_Schema.Processlist where Id={0} AND User='{1}' AND Time >= {2}".format( conn_id, conn.user, self.__command_timeout)) records = cursor.fetchall() if len(records) > 0: cursor.execute('Kill query ' + str(conn_id))