2017-05-29 1 views
0

私はPythonで初めてマルチスレッドを試みています。私はdocumentation hereを参照してください。私のサンプルコードPythonでマルチスレッドを行うpool.mapはTypeErrorを発生させます: 'float'型のオブジェクトにはlen()がありません

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 

それはpool.map関数とリスト(see this tutorial too)をとることは明らかであるドキュメントから、次のエラー

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

を返しますが、以下に示し、それはfloat寄付としてmy_listを想定している理由このエラー。助言がありますか ?ありがとう

答えて

2

このコードは何をしますか?

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または上lenを呼び出していますfloatでしたか?

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

のでpool.mapは機能、リストのすべての権利がかかりますが、あなたの関数mylistにそう

(「分散」ループを実行するために)各スロットのためのリストの1つの要素でmy_method呼び出しますリスト自体ではなく、リストの1つの要素です(ループは暗黙的にmap関数によって実行されるため意味がありません)

関連する問題