简体   繁体   中英

Simple python multiprocessing pool script error

I've tried to get a very simple multiprocessing script to work and am failing to figure out what I'm doing wrong. I'm utilizing Python 2.7.5 64bit on win32. I was looking at Python Multiprocessing help exit on condition for assistance.

Style 1:

import multiprocessing

def doCalc(year):
    return year*year

yearlist = [1,2,3,4]
print(yearlist)

pool = multiprocessing.Pool(4)

for i in yearlist:
    pool.apply_async(doCalc, args=[i])

pool.close()
pool.join()

Style 2:

import multiprocessing

def doCalc(year):
    return year*year

yearlist = [1,2,3,4]
print(yearlist)

pool = multiprocessing.Pool(4)

pool.map(doCalc, yearlist)

Both scripts print [1,2,3,4] and then do nothing. Thanks for any help.

On windows, you should run multiprocessing functions inside if __name__ == "__main__" ( doc1 , doc2 ). So, try this:

import multiprocessing

def doCalc(year):
    return year*year

yearlist = [1,2,3,4]
print(yearlist)

if __name__ == '__main__':
    pool = multiprocessing.Pool(4)
    pool.map(doCalc, yearlist)

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