76

Python用の単純なプロセスベースの並列マップ、つまり関数を探しています

parmap(function,[data])

これは、異なるプロセスの[data]の各要素で関数を実行し(まあ、異なるコアで、しかしAFAIK、Pythonで異なるコアで何かを実行する唯一の方法は、複数のインタープリターを起動することです)、結果のリストを返します。

このようなものはありますか?シンプルなものが欲しいので、シンプルなモジュールがいいでしょう。もちろん、そのようなものが存在しない場合、私は大きな図書館に落ち着きます:-/

4

5 に答える 5

138

必要なのは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 till 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 integ

たとえば、この関数をマップする場合は、次のようにします。

def f(x):
    return x**2

range(10)には、組み込みのmap()関数を使用して実行できます。

map(f, range(10))

またはmultiprocessing.Pool()オブジェクトのメソッドmap()を使用します。

import multiprocessing
pool = multiprocessing.Pool()
print pool.map(f, range(10))
于 2009-11-09T22:49:55.793 に答える