首页 > 解决方案 > 访问 multiprocessing.reduction 时,'bool' 对象不可调用?

问题描述

我正在编写代码以使用 Python 3.x 多处理库并行化任务。

我有两个类,(其内容太大而无法粘贴),以及两个这样的功能:

def abc_1(objekt, raw):
    return raw + "changed"


def file_task(file_name):
    con_fig = Some_Config()
    obj_ect = Some_other_class(con_fig)
    pool = Pool(4)
    with open(file_name, 'r', encoding='utf-8') as fh:
        res = pool.map(partial(abc_1, con_fig), fh, 4)
    return res

当我运行上面的代码时,我得到了这个错误:

cls = <class 'multiprocessing.reduction.ForkingPickler'>
obj = (0, 0, <function mapstar at 0x10abec9d8>, ((functools.partial(<function abc_1 at 0x10ad98620>, <project.some_file.... 'Indeed, Republican lawyers identified only 300 cases of electoral fraud in the United States in a decade.\n')),), {})
protocol = None

@classmethod
def dumps(cls, obj, protocol=None):
    buf = io.BytesIO()
    cls(buf, protocol).dump(obj)

    TypeError: 'bool' object is not callable

但是,如果我像这样更改我的两个函数,我不需要将类的对象发送到我想要并行化的函数 abc_1,它工作正常:

def abc_1(raw):
    return raw + "changed"


def file_task(file_name):
    con_fig = Some_Config()
    obj_ect = Some_other_class(con_fig)
    pool = Pool(4)
    with open(file_name, 'r', encoding='utf-8') as fh:
        res = pool.map(abc_1, fh, 4)
    return res

我认为“部分”是罪魁祸首,但即使这样也有效:

def file_task(file_name):
    con_fig = Some_Config()
    obj_ect = Some_other_class(con_fig)
    pool = Pool(4)
    with open(file_name, 'r', encoding='utf-8') as fh:
        res = pool.map(partial(abc_1), fh, 4)
    return res

因为,我没有提供我在课堂上的内容,所以我知道他很难回答我的问题。

我浏览过几个这样的帖子:TypeError: 'bool' object is not callable while creating custom thread pool

我的代码中没有发生这种情况,甚至在我的课程中也没有。

我在课堂上的初始化会导致我得到的错误吗?还有什么可能导致这种情况?在多处理期间将哪些类实例传递给函数需要记住哪些事项?

如果有人能指出我正确的方向,那将真的很有帮助。

标签: pythonmultiprocessing

解决方案


推荐阅读