python - python fetchall()中的MySQL死锁

标签 python mysql

我在 PyCharm 中有这段代码。我点击“播放”来运行代码,它工作正常,但如果我点击两次或更多次,有时不起作用,程序进入死锁行:

f=cnx_cursor1.fetchall()

代码:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

import mysql.connector
import time
tik = time.time()

configDB = {
        'user': '****',
        'password': '****',
        'host': '****',
        'database': '****',
        'raise_on_warnings': True,
        'charset': 'utf8',
        'use_unicode': True,
        'collation': 'utf8_general_ci'
    }

cnx = mysql.connector.connect(**configDB)
cnx_cursor1 = cnx.cursor(dictionary=True)
sql1 = "SELECT E.*, " + \
                   "    ((E.statusNumber & 4278190080) >> 24) as s1, " + \
                   "    ((E.statusNumber & 0016711680) >> 16) as s2, " + \
                   "    ((E.statusNumber & 0000065280) >> 08) as s3, " + \
                   "    ((E.statusNumber & 0000000255) >> 00) as s4 " + \
                   " from EUser as E order by E.user_id, E.project;"
cnx_cursor1.execute(sql1)
print "After Execute:", "{:3.10f}".format(time.time()-tik), "s"
f=cnx_cursor1.fetchall()
print "After Fetch:", "{:3.10f}".format(time.time()-tik), "s"
ret = []
for l in f:
    print "user_id:", l["user_id"], "s1:", l["s1"], "s2:", l["s2"], "s3:", l["s3"], "s4:", l["s4"]
    if l["statusPhoto"] == 0:
        pass
    time.sleep(1)
cnx_cursor1.close()
cnx.close()
print "END:", "{:3.10f}".format(time.time()-tik), "s"

谁能帮帮我?

这个是MySQL连接的问题?

谢谢

最佳答案

FetchAll 策略有几个问题(我遇到过)

  1. 在通过网络将查询发送给客户端之前,您必须等待查询的所有结果
  2. 它会占用大量内存。在服务器和客户端上
  3. 客户对事情突然变慢的原因知之甚少

下面是我将如何解决这个问题

  1. 出现问题时,使用管理工具访问您的 MySQL 实例并查找事件请求。如果您看到您的请求,它应该可以深入了解为什么需要这么长时间。也许有一个行锁?也许你记性不好?也许您的客户停止响应?不看就永远不会知道。
  2. 使用FetchOneFetchMany 获取更易于排查问题的较小批处理的行
    https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
    https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchmany.html
  3. 研究根据您的请求设置超时。也许它被设置为无限(或者超出您想要人工等待的时间),也许您可​​以将它设置为一个较低的数字以强制超时并获得更好的错误信息。

希望对你有帮助

关于python - python fetchall()中的MySQL死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990123/

相关文章:

python - 通过回调更新 Dash 数据表

python - 如果将 DataFrame 保存到磁盘,如果您在脚本中使用该 DataFrame,Spark 是否会加载该数据?

mysql - Laravel 依赖选择值 = 0

php - 如何使用 PHP 从 MySQL 数据库存储和检索图像?

python - 覆盖属性的 getter 和 setter

python - 递增现有属性时调用 __setattr__ 吗?

python - 获取IP地址的前三个字节

php - 排序 html php mysql 表的问题

mysql - 为什么我收到错误代码 1452 无法添加或更新子行

mysql - 当键自动递增时插入忽略?