使用带有一个参数和self的pool.map在类中调用函数:TypeError:map()缺少1个必需的位置参数:“ iterable”

时间:2018-10-05 19:07:29

标签: python threadpool

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

(129,)

该函数除self外没有其他参数,而我收到此错误

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

这是功能

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

编辑:

我缺少要映射的变量self.doc_ids,它是一个列表。

我现在这样称呼它

def get_data_vector(self):

该函数应这样调用

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

但错误现在更改为

def get_data_vector(self, doc_id):

1 个答案:

答案 0 :(得分:1)

我将假设self.doc_ids是列表或其他可迭代的内容。

那么您应该可以使用它:

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

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