MySQL服务器在进行多处理时消失了 - python

时间:2016-04-18 13:10:57

标签: python mysql

我想使用多处理对MySQL DB中的数据做一些事情。每次我运行以下代码时,都会显示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

我做错了什么?

0 个答案:

没有答案