python – 为什么还有“命令不同步;你现在无法运行此命令“错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 为什么还有“命令不同步;你现在无法运行此命令“错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2066字,纯文字阅读大概需要3分钟。
内容图文
我使用Python mysqldb库来连接mysql db.我有一个带有4个工作进程的Web服务器,它有1个conn和1个游标到mysql db.所以每个工作进程都会使用它的连接/游标来执行sql语句.
现在,我有几个客户端同时向服务器发送请求,服务器将查询mysql db,并将一些结果返回给客户端.我遇到错误. 2014年,“命令不同步;您现在无法运行此命令”
我检查sql,它只是简单的SELECT a,b,c来自表WHERE a = 1.没有分号或存储过程,我也尝试下面的代码,如Python, “commands out of sync; you can’t run this command now”建议.但它仍然是同样的错误.
self.cursor.execute(sql, data)
self.conn.commit()
result = result + self.cursor.fetchall()
self.cursor.close()
self.cursor = self.conn.cursor()
解决方法:
最后,我解决了这个问题.我的应用程序有多线程使用相同的连接,似乎不是访问mysql的正确方法,所以当我不共享连接时,问题就消失了.
The MySQL protocol can not handle multiple threads using the same
connection at once. Some earlier versions of MySQLdb utilized locking
to achieve a threadsafety of 2. While this is not terribly hard to
accomplish using the standard Cursor class (which uses
mysql_store_result()), it is complicated by SSCursor (which uses
mysql_use_result(); with the latter you must ensure all the rows have
been read before another query can be executed. It is further
complicated by the addition of transactions, since transactions start
when a cursor execute a query, but end when COMMIT or ROLLBACK is
executed by the Connection object. Two threads simply cannot share a
connection while a transaction is in progress, in addition to not
being able to share it during query execution. This excessively
complicated the code to the point where it just isn’t worth it.The general upshot of this is: Don’t share connections between
threads. It’s really not worth your effort or mine, and in the end,
will probably hurt performance, since the MySQL server runs a separate
thread for each connection. You can certainly do things like cache
connections in a pool, and give those connections to one thread at a
time. If you let two threads use a connection simultaneously, the
MySQL client library will probably upchuck and die. You have been
warned.
内容总结
以上是互联网集市为您收集整理的python – 为什么还有“命令不同步;你现在无法运行此命令“错误全部内容,希望文章能够帮你解决python – 为什么还有“命令不同步;你现在无法运行此命令“错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。