PyMySQL引发了#BrokenSipeError'频繁阅读后

时间:2017-01-14 01:23:00

标签: python mysql python-3.x pymysql

我编写了一个脚本来帮助我使用数据库。具体来说,我正在尝试使用磁盘上的文件,并将此工作的结果添加到我的数据库。我复制了下面的代码,但删除了大部分与我的数据库无关的逻辑,试图保持这个问题的广泛和有用。

我使用代码对文件进行操作并将结果添加到数据库中,覆盖与我正在处理的文件具有相同标识符的任何文件。后来,我修改了脚本以忽略已经添加到数据库的文档,现在每当我运行它时都会出错:

pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")

似乎服务器拒绝了请求,可能是因为我编写的代码很差?我注意到错误总是发生在文件列表中的同一个地方,并没有改变。如果我重新运行运行代码,用仅列出程序崩溃的文件的列表替换文件列表,它可以正常工作。这让我觉得在发出一定数量的请求之后,数据库才刚刚结束。

我在OS X上使用Python 3和MySQL Community Edition版本14.14。

代码(剥离了与数据库无关的内容):

import pymysql

# Stars for user-specific stuff
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='*******',
                             db='*******',
                             use_unicode=True, 
                             charset="utf8mb4",
                             )
cursor = connection.cursor()

f_arr = # An array of all of my data objects

def convertF(file_):
    # General layout: Try to work with input and add it the result to DB. The work can raise an exception
    # If the record already exists in the DB, ignore it
    # Elif the work was already done and the result is on disk, put it on the database
    # Else do the work and put it on the database - this can raise exceptions
    # Except: Try another way to do the work, and put the result in the database. This can raise an error
    # Second (nested) except: Add the record to the database with indicator that the work failed

    # This worked before I added the initial check on whether or not the record already exists in the database. Now, for some reason, I get the error:
    # pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")

    # I'm pretty sure that I have written code to work poorly with the database. I had hoped to finish this task quickly instead of efficiently.
    try:
        # Find record in DB, if text exists just ignore the record
        rc = cursor.execute("SELECT LENGTH(text) FROM table WHERE name = '{0}'".format(file_["name"]))
        length = cursor.fetchall()[0][0] # Gets the length
        if length != None and length > 4:
            pass
        elif ( "work already finished on disk" ): 
            # get "result_text" from disk
            cmd = "UPDATE table SET text = %s, hascontent = 1 WHERE name = %s"
            cursor.execute(cmd, ( pymysql.escape_string(result_text), file_["name"] ))
            connection.commit()
        else:
            # do work to get result_text
            cmd = "UPDATE table SET text = %s, hascontent = 1 WHERE name = %s"
            cursor.execute(cmd, ( pymysql.escape_string(result_text), file_["name"] ))
            connection.commit()
    except:
        try: 
            # Alternate method of work to get result_text
            cmd = "UPDATE table SET text = %s, hascontent = 1 WHERE name = %s"
            cursor.execute(cmd, ( pymysql.escape_string(result_text), file_["name"] ))
            connection.commit()
        except:
            # Since the job can't be done, tell the database
            cmd = "UPDATE table SET text = %s, hascontent = 0 WHERE name = %s"
            cursor.execute(cmd, ( "NO CONTENT", file_["name"]) )
            connection.commit()

for file in f_arr:
    convertF(file)

2 个答案:

答案 0 :(得分:3)

Mysql Server已经消失了

Parallel::ForkManager广泛描述了这个问题,通常的原因是服务器因任何原因断开连接,通常的补救措施是重试查询或重新连接并重试。

但是为什么这会破坏你的代码是因为你编写代码的方式。见下文

可能是因为我写的代码很差?

你问过。

rc = cursor.execute("SELECT LENGTH(text) FROM table WHERE name = '{0}'".format(file_["name"]))

这是一个坏习惯。手动显式警告您不要这样做以避免SQL注入。正确的方法是

 rc = cursor.execute("SELECT LENGTH(text) FROM table WHERE name = %s", (file_["name"],))

上述代码的第二个问题是,在尝试更新之前,您无需检查值是否存在。您可以删除上面的行,如果是,则关联它并直接跳转到更新。此外,我们的elifelse似乎完全相同。所以你的代码可以只是

try:
        cmd = "UPDATE table SET text = %s, hascontent = 1 WHERE name = %s"
        cursor.execute(cmd, ( pymysql.escape_string(result_text), file_["name"] ))
        connection.commit()
except:  # <-- next problem.

我们来到下一个问题。永远不会捕获这样的通用异常。你应该总是捕获TypeError,AttributeError等特定异常。当捕获泛型异常是不可避免的时,你至少应该记录它。

例如,在这里您可以捕获连接错误并尝试重新连接到数据库。然后,当您的服务器消失时,代码将不会停止执行。

答案 1 :(得分:1)

当我尝试通过减少要在一个命令中插入的行数来进行批量插入时,已经解决了相同的错误。

即使批量插入的最大行数要高得多,我也会遇到这种错误。