Python's multiprocessing map_async generates error on Windows

时光毁灭记忆、已成空白 提交于 2020-01-02 05:44:48

问题


The code below works perfectly on Unix but generates a multiprocessing.TimeoutError on Windows 7 (both OS use python 2.7).

Any idea why? Thanks.

from multiprocessing import Pool

def increment(x):
    return x + 1

def decrement(x):
    return x - 1

pool = Pool(processes=2)
res1 = pool.map_async(increment, range(10))
res2 = pool.map_async(decrement, range(10))

print res1.get(timeout=1)
print res2.get(timeout=1)

回答1:


You need to put your actual program logic in side a if __name__ == '__main__': block.

On Unixy systems, Python forks, producing multiple processes to work from. Windows doesn't have fork. Python has to launch a new interpreter and re-import all your modules instead. This means that each subprocess will reimport your main module. For the code you've written reimporting the module will cause each newly launched processes to launch processes of its own.

See: http://docs.python.org/library/multiprocessing.html#windows

EDIT this works for me:

from multiprocessing import Pool

def increment(x):
    return x + 1

def decrement(x):
    return x - 1

if __name__ == '__main__':
    pool = Pool(processes=2)
    res1 = pool.map_async(increment, range(10))
    res2 = pool.map_async(decrement, range(10))

    print res1.get(timeout=1)
    print res2.get(timeout=1)


来源:https://stackoverflow.com/questions/12465270/pythons-multiprocessing-map-async-generates-error-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!