Multithreading in python pool.map raises TypeError: object of type 'float' has no len()

Multithreading in python pool.map raises TypeError: object of type 'float' has no len()

我在 Python 中第一次尝试多线程。我看到 documentation here。以下是我的示例代码

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 

就是returns下面的错误

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()

从文档中可以清楚地看出,pool.map 采用函数和列表 (see this tutorial too),但为什么它假设 my_list 作为 float 给出此错误。有什么建议么 ?谢谢

这段代码有什么作用?

results = pool.map(my_method, my_list)

它多次调用 my_method,每次从您的列表中传入一个元素 my_list 参见: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.

所以

for i in range (len(mylist)):

实际上是在 intfloat 上调用 len 你是不是想说

for i in range (mylist):
mylist = [3,4,5,6,7,8, ..., 5]
results = pool.map(my_method, my_list)

所以 pool.map 接受一个函数和一个列表,但是调用 my_method 每个槽用列表的一个元素(执行 "distributed" 循环)

所以在你的函数中 mylist 是列表的一个元素,而不是列表本身(并且在它上面循环是没有意义的,因为循环是由 map 函数隐式完成的)