I am trying to use the mutltiprocessing package to use multiple CPUs within a function. When I run a toy example outside of a function it runs in a quarter of a second with no problems (see below).
from multiprocessing import Pool
import time
start = time.clock()
def f(x):
return x*x
if __name__ == '__main__':
with Pool(processes=7) as pool:
result = pool.map(f, range(1000))
print(time.clock() - start)
However, when I adapt the same code into a function (see below), it prints True to indicate that __name__ == '__main__', but then it runs forever and never returns a result. I am running Python 3.3 on Windows 7.
from multiprocessing import Pool
import time
start = time.clock()
def f(x):
return x*x
def testfunc(r):
if __name__ == '__main__':
print(True)
with Pool(processes=7) as pool:
result = pool.map(f, range(r))
return result
result = testfunc(1000)
print(time.clock() - start)
if __name__ == '__main__'block outside the function, around the call totestfunc.AttributeError: __exit__. I cannot even get the first example to run... Am I missing something?