In Python I have seen many examples where multiprocessing is called but the target just prints something. I have a scenario where the target returns 2 variables, which I need to use later. For example:
def foo(some args): a = someObject b = someObject return a,b p1=multiprocess(target=foo,args(some args)) p2=multiprocess(target=foo,args(some args)) p3=multiprocess(target=foo,args(some args))
Now what? I can do .start and .join, but how do I retrieve the individual results? I need to catch the return a,b for all the jobs I execute and then work on it.
Multiprocessing can dramatically improve processing speedPython's built-in multiprocessing module allows us to designate certain sections of code to bypass the GIL and send the code to multiple processors for simultaneous execution.
Multiprocess is a Python package that supports spawning processing tasks using an API similar to the Python threading module. In addition, the multiprocessing package supports concurrency in both local and remote types, allowing you to bypass the global interpreter lock that comes with threading.
First, we create two Process objects and assign them the function they will execute when they start running, also known as the target function. Second, we tell the processes to go ahead and run their tasks. And third, we wait for the processes to finish running, then continue with our program.
You are looking to do some embarrassingly parallel work using multiple processes, so why not use a Pool
? A Pool
will take care of starting up the processes, retrieving the results, and returning the results to you.
I use pathos
, which has a fork of multiprocessing
, because it has much better serialization than the version that standard library provides.
(.py) file
from pathos.multiprocessing import ProcessingPool as Pool def foo(obj1, obj2): a = obj1.x**2 b = obj2.x**2 return a,b class Bar(object): def __init__(self, x): self.x = x Pool().map(foo, [Bar(1),Bar(2),Bar(3)], [Bar(4),Bar(5),Bar(6)])
Result
[(1, 16), (4, 25), (9, 36)]
And you see that foo
takes two arguments, and returns a tuple of two objects. The map
method of Pool
submits foo
to the underlying processes and returns the result as res
.
You can get pathos
here: https://github.com/uqfoundation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With