Вопрос

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 insideif __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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top