This is a follow-up question for the question I asked here. I tried to parallelize my code as follows:
import concurrent.futures as futures
class A(object):
def __init__(self, q):
self.p = q
def add(self, num):
r = 0
for _ in xrange(10000):
r += num
return r
num_instances = 5
instances = []
for i in xrange(num_instances):
instances.append(A(i))
n = 20
# Create a pool of processes. By default, one is created for each CPU in your machine.
results = []
pool = futures.ProcessPoolExecutor(max_workers=num_instances)
for inst in instances:
future = pool.submit(inst.add, n)
results.append(future.result())
pool.join()
print(results)
But, I got this error:
Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 268, in _feed send(obj) PicklingError: Can't pickle : attribute lookup builtin.instancemethod failed
Any idea why I get this error? I know we can use map function to assign jobs, but I intentionally don't want to do that.