Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to multiprocess a function that returns something in Python?

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.

like image 304
Nishant Avatar asked May 29 '12 11:05

Nishant


People also ask

Is multiprocessing possible in Python?

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.

What is multiprocess Python?

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.

How do you parallelize a code in Python?

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.


1 Answers

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

like image 62
Mike McKerns Avatar answered Sep 22 '22 04:09

Mike McKerns