_mysql_exceptions.ProgrammingError:(2014, "commands out of sync; you can't run this command now")

今天,测试dashboard上的一些graph, 发现,当多个graph同时向后台请求数据(异步)的时候, 出现了上述错误。而且,三个bug交替出现,另外两个bug分别是:python stop response 和mysql close closed connection。

查到最后,发现,因为ajax是异步请求的,而后台处理的时候,建立了一个connection以后,执行多条sql语句的时候,就出现了上述错误。

出错地方:

  

错误代码:  

    def ExceptionHandling_mysql(self,sql):
    try:
        cur = self.conn.cursor(libdb.cursors.DictCursor)
        rowcount = cur.execute(sql)
        self.conn.commit()
        result = cur.fetchall()
        while cur.nextset(): pass            
        #if cur:    
        #cur.close()    
    except Exception, e:
        print 'This is mysql: %s' % (sql)
        print e
        result = []
        
        #self.conn = libdb.connect(host=configeApp.getDBHost(), port=configeApp.getDBPort(), user=configeApp.getUserName(), 
                             #passwd=configeApp.getPwd(), db=configeApp.getDBase(), use_unicode=True, charset='utf8')            
    if cur:
        cur.close()
    if self.conn:
        self.conn.close()        
    return result
    

最终问题解决,改成一个connection,进行一个sql语句的查询。

原文地址:https://www.cnblogs.com/xiami303/p/3309391.html