简体   繁体   English

Windows和Spyder中的python多重处理问题

[英]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. 我有一个关于python多重处理的大学项目,对于python项目,我在Windows中使用spyder。 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. 因此,我试图在spyder中运行非常琐碎的多处理代码,但每次运行spyder控制台时,它都会冻结并且永远无法完成。 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中没有fork,因此我考虑了16.6.3.2段。 Windows in https://docs.python.org/2/library/multiprocessing.html#windows . Windows, 网址为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. 我还避免了如下所述从子进程中进行打印: 简单的Python Multiprocessing函数不会输出结果 ,而是使用return。

My code works if i run it from a unix terminal, but i'm mainly using windows for my python projects. 如果我从unix终端运行它,我的代码将起作用,但是我主要是将Windows用于python项目。 Is there a workaround for this issue in windows? 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. https://docs.python.org/2.7/library/multiprocessing.html#introduction中指出: 注意 :...某些示例(例如Pool示例)在交互式解释器中不起作用。

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. spyder维护人员回答了一个较旧的文章,该文章解决了类似的问题,即在Windows的Spyder IPython控制台中,多处理确实无法正常工作。 ( No multiprocessing print outputs (Spyder) ) 没有多处理打印输出(Spyder)

The only workaround i found so far is to use the windows cmd to run the code 到目前为止,我发现的唯一解决方法是使用Windows cmd运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM