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 simple brute force integer factoring program where I can factor 1 integer on each core (4)? my real program probably only needs 2 cores, and need to share information.
I know that parallel-python and other libraries exist, but i want to keep the number of libraries used to a minimum, thus I want to use the thread
and/or multiprocessing
libraries, since they come with python
I agree that using
Pool
frommultiprocessing
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 asmultiprocessing
), then you could trypathos
, which provides several forms of parallel maps and has pretty much the same interface asmultiprocessing
does.Also,
pathos
has a sister package with the same interface, calledpyina
, which runsmpi4py
, 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 thanmultiprocessing
at serializing a range of functions and other things. And you can do everything from the interpreter.Get the code here: https://github.com/uqfoundation
This can be done elegantly with Ray, a system that allows you to easily parallelize and distribute your Python code.
To parallelize your example, you'd need to define your map function with the
@ray.remote
decorator, and then invoke it with.remote
. This will ensure that every instance of the remote function will executed in a different process.There are a number of advantages of using Ray over the multiprocessing module. In particular, the same code will run on a single machine as well as on a cluster of machines. For more advantages of Ray see this related post.
mincemeat
is the simplest map/reduce implementation that I've found. Also, it's very light on dependencies - it's a single file and does everything with standard library.A good simple way to start with parallel processing in python is just the pool mapping in mutiprocessing -- its like the usual python maps but individual function calls are spread out over the different number of processes.
Factoring is a nice example of this - you can brute-force check all the divisions spreading out over all available tasks:
This gives me