Python: MySQL: Handling timeouts

后端 未结 3 1395
小鲜肉
小鲜肉 2021-01-13 09:02

I am using Python and mySQL, and there is a long lag between queries. As a result, I get an \'MySQL connection has gone away\' error, that is wait_timeout is exceeded.

3条回答
  •  广开言路
    2021-01-13 09:57

    I was running into mystifying "MySQL server has gone away" errors, and here is my solution.

    This solution will let you retry through MySQL errors, handle pretty much any type of query, include query variables in the query str or in a separate tuple, and collect and return all of the success and error messages you encounter along the way:

    def execute_query(query_str, values=None):
      # defaults
      num_affected_rows = 0
      result_rows = None
      success = False
      message = "Error executing query: {}".format(query_str)
      # run the query
      try:
        mysql_conn = get_existing_mysql_connection()
        cur = mysql_conn.cursor()
        if values == None or len(values) < 1:
          num_affected_rows = cur.execute(query_str)
        else:
          num_affected_rows = cur.execute(query_str, values)
        result_rows = cur.fetchall() # only relevant to select, but safe to run with others
        cur.close()
        mysql_conn.commit()
        success = True
        message = "Mysql success for query: {}".format(query_str)
      except BaseException as e:
        message = "Mysql error: {}; for query: {}".format(repr(e), query_str)
      return (success, num_affected_rows, result_rows, message)
    
    
    def execute_query_with_retry(query_str, values=None, num_tries=3, message=""):
      # defaults
      success = False
      num_affected_rows = 0
      result_rows = None
      this_message = "Error executing query: {}".format(query_str)
      # should we still try?
      if num_tries < 1:
        this_message = "Ran out of tries for query: {}".format(query_str)
        return (False, 0, None, message + '; ' + this_message)
      num_tries_after_this = num_tries - 1
      # try to execute query
      try:
        (success, num_affected_rows, result_rows, this_message) = execute_query(query_str, values)
      except BaseException as e:
        success = False
      # handle success or failure
      if success == True:
        return (True, num_affected_rows, result_rows, message + '; ' + this_message)
      else:
        open_new_mysql_connection() # reconnect using password etc.
        return(execute_query_with_retry(query_str, values=values, num_tries=num_tries_after_this, message=(message + '; ' + this_message)))
    

提交回复
热议问题