I have the following code.
def main():
(minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
for i in range(minI, maxI, iStep):
for
The reason you are most likely seeing this behavior is because of the order in which you define your pool, objects, and functions. multiprocessing
is not quite the same as using threads. Each process will spawn and load a copy of the environment. If you create functions in scopes that may not be available to the processes, or create objects before the pool, then the pool will fail.
First, try creating one pool before your big loop:
(minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
pool = multiprocessing.Pool(processes=numProcessors)
for i in range(minI, maxI, iStep):
...
Then, move your target callable outside the dynamic loop:
def functionB(a, b):
...
def main():
...
Consider this example...
broken
import multiprocessing
def broken():
vals = [1,2,3]
def test(x):
return x
pool = multiprocessing.Pool()
output = pool.map(test, vals)
print output
broken()
# PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
working
import multiprocessing
def test(x):
return x
def working():
vals = [1,2,3]
pool = multiprocessing.Pool()
output = pool.map(test, vals)
print output
working()
# [1, 2, 3]