You can use a Pool
as described in the example you provided. This sort of works, but you have to remove the None
result afterwards:
import multiprocessing as mp
class Thing:
def __init__(self, y):
self.attribute = y
def square(thing, given_attribute):
if thing.attribute == given_attribute:
return thing
given_attribute = 4
x = [Thing(i) for i in range(10)] # List of objects to process
if __name__ == '__main__':
pool = mp.Pool(processes=4)
results = [pool.apply(square, args=(x[i], given_attribute, )) for i in range(10)]
r = [i for i in results if i is not None] # Remove the None results
print r