【问题标题】:python multiprocessing issue in windows and spyderwindows和spyder中的python多处理问题
【发布时间】:2019-06-14 17:05:25
【问题描述】:

我的大学有一个关于 python 多处理的项目。对于我的 python 项目,我在 windows 中使用 spyder。因此,我试图在 spyder 中运行一个非常简单的多处理代码,但每次我运行它时,spyder 控制台都会冻结并且永远不会完成。这是我的代码:

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()

我注意到这是多进程和 windows 中缺少 fork 的常见问题,因此我考虑了第 16.6.3.2 段。 https://docs.python.org/2/library/multiprocessing.html#windows 中的窗口。

我还避免从我的子进程中打印,如下所述:Simple Python Multiprocessing function doesn't output results,而是使用了 return。

如果我从 unix 终端运行它,我的代码可以工作,但我主要将 windows 用于我的 python 项目。 Windows 中是否有解决此问题的方法?

【问题讨论】:

  • 您是否尝试过从 Windows 命令提示符运行?还是使用 Python 版本 3?我尝试从 Windows 10 命令提示符 Python 版本 3 运行您的代码,它可以工作。
  • 我有,而且效果很好。我试图为 spyder 找到解决方案或解决方法。经过一番研究,我发现这是一个现场翻译问题。但我想不使用 spyder 进行多处理并不完全是一个编程答案。
  • 不管怎样,我可以在其 ipython 解释器中的 win7、py2.7、spyder 3.1.3 上运行您的代码
  • @kevinsa5 我在 win10,python2.7,spyder 3.2.6 上,问题仍然存在。 Here 一位 spyder 维护人员表示,在 Spyder 的 IPython 控制台中,确实多处理在 Windows 上无法正常工作
  • @vtsimp 您能否在 Spyder 的 GitHub 存储库上打开一个关于您的问题的问题。我会在那边回答你的问题。

标签: python spyder python-multiprocessing


【解决方案1】:

经过一些研究,我了解到交互式解释器和多处理存在问题。

在:https://docs.python.org/2.7/library/multiprocessing.html#introduction 中声明: 注意:...一些示例,例如 Pool 示例在交互式解释器中不起作用。

一篇较早的帖子解决了一个类似的问题,一位 spyder 维护人员回答说,确实多处理在 Spyder 的 IPython 控制台中的 Windows 上不能很好地工作。 (No multiprocessing print outputs (Spyder))

目前我发现的唯一解决方法是使用 windows cmd 运行代码

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 2019-02-07
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多