How to call class methods from different instantiations in parallel in Python 2.7?
<p>I have a class which contains functions that perform calculations. If I have several instances of these objects, how do I make the calculations to go in parallel?</p>

<pre><code>class SomeClass:
    def __init__(self, arg):
    ....
    def compute(self):
    ....
</code></pre>

<p>And then in a different script:</p>

<pre><code>from multiprocessing import Pool
from wherever import SomeClass
    g1 = SomeClass(arg1)
    g2 = SomeClass(arg2)
    pool = Pool(processes = 2)
</code></pre>

<p>How do I make one of the workers do g1.compute() and the other g2.compute()?</p>

<p>On python2.7 you'll have to define a worker function that calls the compute method on the given argument, then you can use it with <code>pool.map()</code>:</p>

<pre><code>def call_compute(o):
    return o.compute()
...
result = pool.map(call_compute, [g1, g2])
</code></pre>

<p>On python3 (tested on 3.5) it's possible to use <code>pool.map(SomeClass.comute, [g1, g2])</code> instead because pickling instance methods is supported there.</p>

python python-2.7 multiprocessing python-multiprocessing