简单的python多处理池脚本错误

时间:2014-04-17 19:08:20

标签: python multiprocessing

我试图让一个非常简单的多处理脚本工作,但我没有弄清楚我做错了什么。我在win32上使用Python 2.7.5 64位。我正在寻找Python Multiprocessing help exit on condition寻求帮助。

风格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()

风格2:

import multiprocessing

def doCalc(year):
    return year*year

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

pool = multiprocessing.Pool(4)

pool.map(doCalc, yearlist)

两个脚本都打印[1,2,3,4]然后什么都不做。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

在Windows上,您应该在if __name__ == "__main__"doc1doc2)中运行多处理功能。所以,试试这个:

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)