python multiprocessing issue in windows and spyder

心已入冬 提交于 2021-02-04 16:44:08

问题


I have a project for my college regarding multiprocessing in python.For my python projects I use spyder in windows. Therefore im trying to run a very trivial multi - processing code in spyder but every time i run it spyder console freezes and never finishes. This is my code:

from multiprocessing import Pool, freeze_support
import multiprocessing

def f(i):
    return i+1

def main():
    pool = multiprocessing.Pool(4)
    result = pool.map(f, range(4))
    pool.close()
    pool.join()
    print result

if __name__ == '__main__':
    freeze_support() #you need this in windows
    main()

I have noticed this is a common issue with multi-proccesing and the lack of fork in windows, therefore i took into account the paragraph 16.6.3.2. Windows in https://docs.python.org/2/library/multiprocessing.html#windows .

I also avoided printing from my child process as stated here:Simple Python Multiprocessing function doesn't output results and instead used return.

My code works if i run it from a unix terminal, but i'm mainly using windows for my python projects. Is there a workaround for this issue in windows?


回答1:


After some research i understand that there is an issue with interactive interpreters and multiporcessing.

In: https://docs.python.org/2.7/library/multiprocessing.html#introduction it is stated that: Note: ... some examples, such as the Pool examples will not work in the interactive interpreter.

An older post addressing a similar issue it was answered by a spyder maintainer that indeed multiprocessing doesn't work well on Windows in Spyder's IPython console. (No multiprocessing print outputs (Spyder))

The only workaround i found so far is to use the windows cmd to run the code



来源:https://stackoverflow.com/questions/52763746/python-multiprocessing-issue-in-windows-and-spyder

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