1

I am trying multithreading for the first time in Python. I see documentation here. Following is my sample code

from multiprocessing.dummy import Pool as ThreadPool

pool = ThreadPool(4)
mylist = [3,4,5,6,7,8, ..., 5]
results = pool.map(my_method, my_list) # from docs it is clear that pool.map takes method and argument as a list 

def my_method(mylist):
    data = []
    for i in range (len(mylist)):
        # do something using mylist = subdata
        data.append(subdata)
    return data 

It returns the following error

Traceback (most recent call last):
  File "C:/Users/algorithm.py", line 30, in my_method
    for i in range (len(mylist)):
TypeError: object of type 'float' has no len()

From the documentation it is clear that pool.map takes function and a list (see this tutorial too) but why it is assuming my_list as a float giving this error. Any suggestions ? Thanks

muazfaiz
  • 4,611
  • 14
  • 50
  • 88

2 Answers2

3

What does this bit of code do?

results = pool.map(my_method, my_list)

It calls my_method multiple times, each times passing in a single element from your list my_list see: https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map

map(func, iterable[, chunksize]) A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.

This method chops the iterable into a number of chunks which it submits to the process pool as separate tasks. The (approximate) size of these chunks can be specified by setting chunksize to a positive integer.

So

for i in range (len(mylist)):

is actually calling len on an int or float did you perhaps mean

for i in range (mylist):
e4c5
  • 52,766
  • 11
  • 101
  • 134
2
mylist = [3,4,5,6,7,8, ..., 5]
results = pool.map(my_method, my_list)

so pool.map takes a function and a list all right, but calls my_method with one element of the list for each slot (to perform a "distributed" loop)

So in your function mylist is one element of the list, not the list itself (and looping on it makes no sense since the loop is implictly done by the map function)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219