python - 访问 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
我的代码中没有发生这种情况,甚至在我的课程中也没有。
我在课堂上的初始化会导致我得到的错误吗?还有什么可能导致这种情况?在多处理期间将哪些类实例传递给函数需要记住哪些事项?
如果有人能指出我正确的方向,那将真的很有帮助。
解决方案
推荐阅读
- sql - ASP 以 HH:mm 格式绑定 SQL 时间(5)
- r - NbClust nstart 和 iter.max 选项
- c++ - 全局声明变量的 C++ 多定义错误
- django - 使用 celery 应用程序部署 Django 的最佳实践
- css - 在清晰数据网格单元格中居中内容
- oracle - 将烛光时间转换为预言机日期时间
- asp.net - 没有 RDC 的远程应用程序连接到 SQL Server
- django - 通过 API 创建用户的 Postman 帖子会导致 CSRF 错误 - 为什么会这样?
- arrays - 为什么 AS3 在 for 循环中创建新实例?
- python - numpy fill_between() 不同的对角线值