简体   繁体   中英

python multiprocessing issue in windows and spyder

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?

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

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