Skip to main content
 首页 » 编程设计

python之python pool.map 中的多线程引发 TypeError : object of type 'float' has no len()

2025年05月04日31zdz8207

我第一次在 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  

它返回以下错误

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

实际上是在 int 或 float 上调用 len 是不是你的意思

for i in range (mylist):