Parallel Processing in python

前端 未结 4 2013
南方客
南方客 2020-11-28 04:29

Whats a simple code that does parallel processing in python 2.7? All the examples Ive found online are convoluted and include unnecessary codes.

how would i do a sim

4条回答
  •  星月不相逢
    2020-11-28 05:21

    I agree that using Pool from multiprocessing is probably the best route if you want to stay within the standard library. If you are interested in doing other types of parallel processing, but not learning anything new (i.e. still using the same interface as multiprocessing), then you could try pathos, which provides several forms of parallel maps and has pretty much the same interface as multiprocessing does.

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

    Also, pathos has a sister package with the same interface, called pyina, which runs mpi4py, but provides it with parallel maps that run in MPI and can be run using several schedulers.

    One other advantage is that pathos comes with a much better serializer than you can get in standard python, so it's much more capable than multiprocessing at serializing a range of functions and other things. And you can do everything from the interpreter.

    >>> 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]
    >>> 
    

    Get the code here: https://github.com/uqfoundation

提交回复
热议问题