verir ben katılıyorum Pool
kullanarak Standart kütüphanede kalmak istiyorsanız, multiprocessing
'dan muhtemelen en iyi yoldur. Paralel işlemenin diğer türlerini yapmakla ilgileniyorsanız, ancak yeni bir şey öğrenmeyle ilgileniyorsanız (yani multiprocessing
ile aynı arabirimi kullanarak), paralel haritalar çeşitli formları sağlar ve multiprocessing
ile hemen hemen aynı arabirimi olan pathos
deneyebilirsiniz yapar. Ayrıca
Python 2.7.6 (default, Nov 12 2013, 13:26:39)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numToFactor = 976
>>> def isFactor(x):
... result = None
... div = (numToFactor/x)
... if div*x == numToFactor:
... result = (x,div)
... return result
...
>>> from pathos.multiprocessing import ProcessingPool as MPool
>>> p = MPool(4)
>>> possible = range(1,int(numpy.floor(numpy.sqrt(numToFactor)))+1)
>>> # standard blocking map
>>> result = [x for x in p.map(isFactor, possible) if x is not None]
>>> print result
[(1, 976), (2, 488), (4, 244), (8, 122), (16, 61)]
>>>
>>> # asynchronous map (there's also iterative maps too)
>>> obj = p.amap(isFactor, possible)
>>> obj
<processing.pool.MapResult object at 0x108efc450>
>>> print [x for x in obj.get() if x is not None]
[(1, 976), (2, 488), (4, 244), (8, 122), (16, 61)]
>>>
>>> # there's also parallel-python maps (blocking, iterative, and async)
>>> from pathos.pp import ParallelPythonPool as PPool
>>> q = PPool(4)
>>> result = [x for x in q.map(isFactor, possible) if x is not None]
>>> print result
[(1, 976), (2, 488), (4, 244), (8, 122), (16, 61)]
,
pathos
mpi4py
çalışır
pyina
denilen aynı arayüzü ile kardeş paketi, var ama MPI çalıştırmak ve birkaç schedulers kullanılarak çalıştırılabilir paralel haritalar birlikte açar.
Diğer bir avantajı, pathos
'un standart python'a göre çok daha iyi bir serileştirici ile birlikte gelmesidir, bu yüzden multiprocessing
'dan çok çeşitli işlevleri ve diğer şeyleri serileştirmede çok daha yeteneklidir. Ve tercümandan her şeyi yapabilirsin.
>>> class Foo(object):
... b = 1
... def factory(self, a):
... def _square(x):
... return a*x**2 + self.b
... return _square
...
>>> f = Foo()
>>> f.b = 100
>>> g = f.factory(-1)
>>> p.map(g, range(10))
[100, 99, 96, 91, 84, 75, 64, 51, 36, 19]
>>>
burada kodu alma: Burada https://github.com/uqfoundation
başka bir yol, ben burada açıkladım geçerli: - [Asgari aktif dişli İşlemler] [1] [1]: http://stackoverflow.com/a/32337959/4850220 –