Parallelization of a class method function call by class objects [Python]

Viewed 75

I want to parallelize the member function call for a list of class objects. I have tried joblib and multiprocessing but they do not seem to work. Could someone take a look and tell me where the problem lies.

from joblib import Parallel, delayed
import multiprocessing as mp


class Test:
    def __init__(self, x):
        self.x = x
        self.y = None

    def set_y(self, function):
        self.y = function(self.x)


def function(x):
    return x ** 2


a = [1, 2, 3, 4]
tests = [Test(i) for i in a]
######### serial code ###########
print('Serial code')
for test in tests:
    test.set_y(function)
    print(test.x, test.y)
######## joblib ############
print('JOBLIB')
tests = [Test(i) for i in a]
Parallel(2)(delayed(Test.set_y)(test, function) for test in tests)
for test in tests:
    print(test.x, test.y)
######### multiprocessing #########
print("MP")
tests = [Test(i) for i in a]
pool = mp.Pool(2)
pool.starmap(Test.set_y, [(test, function) for test in tests])
for test in tests:
    print(test.x, test.y)

And the output looks like follows:

Serial code
1 1
2 4
3 9
4 16
JOBLIB
1 None
2 None
3 None
4 None
MP
1 None
2 None
3 None
4 None

The serial code runs fine but the parallelized code is not working as it should. Please help.

0 Answers
Related