首页 > 解决方案 > 不能将 SQLite Row 对象与多处理库一起使用

问题描述

我正在尝试学习如何在 Python 中使用 SQLite 和多处理库。也许我的问题很简单,但我不知道如何更改我的程序以使其正常工作。

我有以下代码:

import multiprocessing
import time

from db import get_db

def _getDbData():
    db = get_db()
    rows = db.execute('SELECT * FROM table')

    return rows.fetchall()


def _taskPut(row_tuple, queue, flag):
    for i in range(5):
        queue.put((row_tuple[0], i))
        print(' * inserted values: ', row_tuple[0], i)
        time.sleep(0.75)


def _taskGet(queue):
    while queue.qsize():
        print(queue.get())


def runTasks():
    consoles= _getDbData()

    with multiprocessing.Manager() as process_mgr:
        # Create shared Queue and iterable with function arguments
        events_queue= process_mgr.Queue()
        args= zip( [tuple(i) for i in consoles], [events_queue]*len(consoles), [False]*len(consoles) )

        pool= multiprocessing.Pool()
        pool.map(_taskPut, zip(consoles, args))
        pool.map(_takGet, (events_queue,))
        pool.close()
        pool.join()

我收到错误:TypeError: can't pickle sqlite3.Row objects。我不明白为什么会出现此错误,因为我没有通过sqlite3.Row但是tuple.

PS:如果我的英语不完美,请见谅。我不是母语人士。还有函数_taskPut_taskGet是虚拟函数,因为我只是想测试多处理是如何工作的。

标签: python-3.xsqlitemultiprocessing

解决方案


推荐阅读