简体   繁体   中英

Python multiprocessing: calling pool.map within a function

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)

You are using if __name__ == '__main__' in wrong place.

from multiprocessing import Pool
import time

start = time.clock()

def f(x):
    return x*x

def testfunc(r):
    print(True)
    with Pool(processes=7) as pool:     
        result = pool.map(f, range(r))
    return result

if __name__ == '__main__':
    result = testfunc(1000)
    print(time.clock() - start)

According to multiprocessing - Programming guidelines :

Safe importing of main module

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

... one should protect the “entry point” of the program by using if __name__ == '__main__': as follows:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM