Search code examples
pythonthreadpool

Calling a function within a class with pool.map with one argument and self: TypeError: map() missing 1 required positional argument: 'iterable'


I'm trying to call a function within a class from another function within the same class with pool map

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

The function has no arguments except self and I'm getting this error

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

This is the function

def get_data_vector(self):

EDIT:

I was missing the variable to map which is self.doc_ids and it is a list.

I'm now calling it like this

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

The function should be called like this

def get_data_vector(self, doc_id):

but the error now changed to

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

Solution

  • I will assume that self.doc_ids is a list or something else iterable.

    Then you should be able to use this:

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

    This means that get_data_vector will be called with two arguments. The first one is self, as a bound method, and the second one are the elements of the iterable self.doc_ids.