首页 > 解决方案 > 使用带有一个参数和 self 的 pool.map 在类中调用函数:TypeError:map()缺少1个必需的位置参数:'iterable'

问题描述

我正在尝试使用池映射从同一类中的另一个函数调用一个类中的函数

pool = Pool(num_cores)
res = pool.map(self.get_data_vector())

该函数除了 self 没有参数,我收到此错误

TypeError: map() missing 1 required positional argument: 'iterable'

这是功能

def get_data_vector(self):

编辑:

我错过了要映射的变量 self.doc_ids ,它是一个列表。

我现在这样称呼它

res = pool.map(__class__.get_data_vector,(self,self.doc_ids))

该函数应该像这样调用

def get_data_vector(self, doc_id):

但现在错误变为

TypeError: get_data_vector() missing 1 required positional argument: 'doc_id'

标签: pythonthreadpool

解决方案


我会假设这self.doc_ids是一个列表或其他可迭代的东西。

然后你应该可以使用这个:

res = pool.map(self.get_data_vector, self.doc_ids)

这意味着get_data_vector将使用两个参数调用。第一个是self, 作为绑定方法,第二个是 iterable 的元素self.doc_ids


推荐阅读