在处理python时,MySQL服务器消失了

2021-04-11 15:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用多处理来处理MySQL数据库中的数据。每次我运行以下代码时,它都会显示MySQL server has gone away。如果不进行多处理就可以运行这个程序。在

class A(object):

    def __init__(self):
        self.conn = MySQLdb.connect(**MYSQL_SETTINGS)

    def books_query(self, offset):
        """Return books_query."""
        return 'SELECT * FROM book ORDER BY id limit 1000 offset ' + str(offset)

    def target_func(self, start, end):
        for offset in range(start, end + 1, 1000):
            query = self.books_query(offset)
            cursor = self.conn.cursor()
            cursor.execute(query)
            books = cursor.fetchall()

            # Doing something with this data.

start = 0
obj = A()
for i in xrange(12):
    end = start + 1000
    p = multiprocessing.Process(name='process_' + str(end), target=obj.target_func, args=(start, end))
    p.start()
    start = end + 1

我做错什么了?在