Mysql + django异常:“命令不同步;你现在无法运行此命令”

时间:2012-12-28 06:40:45

标签: mysql django mysql-python amazon-rds

通过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参数,也没有在我在这里发布的其他答案中指出的任何其他内容。

任何关于如何调试的想法或建议都将受到赞赏。

1 个答案:

答案 0 :(得分:-3)

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

你需要做类似的事情:

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