通过gunicorn运行django到RDS(AWS mysql),我在我的gunicorn日志中看到此错误:

Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x690ecd0>> ignored

我还不能可靠地重现它,也不能追踪导致它的基础代码。

我在某些地方使用原始游标,遵循这种模式:
cursor = connections['read_only'].cursor()
sql = "select username from auth_user;"
cursor.execute(sql)
rows = cursor.fetchall()
usernames = []
for row in rows:
    usernames.append(row[0])

在某些地方,我立即将游标重新用于另一个查询execute()/fetchall()模式。有时候我不知道。

我还在某些地方使用原始管理器查询。

我没有明确关闭游标,但是我不相信我应该这样做。

除此之外:我没有使用任何存储过程,没有init_command参数,也没有在这里看到的其他答案中指出的任何其他内容。

任何想法或建议如何调试将不胜感激。

最佳答案

查看https://code.djangoproject.com/ticket/17289

您需要执行以下操作:

while cursor.nextset() is not None:
    if verbose:
        print "rows modified %s" % cursor.rowcount

关于MySQL + Django异常: "Commands out of sync; you can' t run this command now",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14065676/

10-09 00:58
查看更多