【问题标题】:Multiprocessing with numpy makes Python quit unexpectedly on OSX使用 numpy 进行多处理使 Python 在 OSX 上意外退出
【发布时间】:2013-10-31 11:23:52
【问题描述】:

我遇到了一个问题,即 Python 在使用 numpy 运行多处理时意外退出。我已经隔离了问题,因此我现在可以确认在运行以下代码时多处理工作正常:

import numpy as np
from multiprocessing import Pool, Process
import time
import cPickle as p

def test(args):
    x,i = args
    if i == 2:
        time.sleep(4)
    arr = np.dot(x.T,x)
    print i

if __name__ == '__main__':
    x = np.random.random(size=((2000,500)))
    evaluations = [(x,i) for i in range(5)]
    p = Pool()
    p.map_async(test,evaluations)
    p.close()
    p.join()

当我尝试评估下面的代码时会出现问题。这使得 Python 意外退出:

import numpy as np
from multiprocessing import Pool, Process
import time
import cPickle as p

def test(args):
    x,i = args
    if i == 2:
        time.sleep(4)
    arr = np.dot(x.T,x)
    print i

if __name__ == '__main__':
    x = np.random.random(size=((2000,500)))
    test((x,4)) # Added code
    evaluations = [(x,i) for i in range(5)]
    p = Pool()
    p.map_async(test,evaluations)
    p.close()
    p.join()

请帮助某人。我愿意接受所有建议。谢谢。注意:我试过两台不同的机器,都出现同样的问题。

【问题讨论】:

  • 我使用 WinPython 在 Windows7/64bit 上运行了您的代码。两种情况都执行并没有错误地退出。
  • 对不起。有趣的是它可以在 Windows 上运行。任何苹果用户可以向我解释为什么会发生这种情况?
  • 我可能误导了你,在我对 Apple 的愤怒评论时到处都使用“意想不到的”术语。我非常怀疑您的问题是特定于操作系统的。您能否尝试使用 numpy install 针对干净的 Python 运行您发布的脚本以查看问题是否仍然存在?
  • +1 用于提供一些干净且可运行的代码!
  • 我刚刚在安装了干净的 Python 和 Numpy 的第三个苹果工作站上进行了尝试。不幸的是,这是同样的行为。

标签: python macos numpy multiprocessing


【解决方案1】:

这是 MacOS X 上的多处理和 numpy 的一个已知问题,并且有点重复:

segfault using numpy's lapack_lite with multiprocessing on osx, not linux

http://mail.scipy.org/pipermail/numpy-discussion/2012-August/063589.html

答案似乎是在链接 Numpy 时使用 Apple 加速框架以外的其他 BLAS...不幸:(

【讨论】:

  • 正如@ogrisel 在第一个链接的评论中指出的那样,在新版本的多处理中还有一个forkserver 模式可以解决这个问题。
【解决方案2】:

我想出了解决问题的方法。在初始化多处理实例之前将 Numpy 与 BLAS 一起使用时会出现此问题。我的解决方法是将 Numpy 代码(运行 BLAS)放入单个进程,然后运行多处理实例。这不是一种好的编码风格,但它确实有效。请参见下面的示例:

以下将失败 - Python 将退出:

import numpy as np
from multiprocessing import Pool, Process

def test(x):
    arr = np.dot(x.T,x) # On large matrices, this calc will use BLAS.

if __name__ == '__main__':
    x = np.random.random(size=((2000,500))) # Random matrix
    test(x)
    evaluations = [x for _ in range(5)]
    p = Pool()
    p.map_async(test,evaluations) # This is where Python will quit, because of the prior use of BLAS.
    p.close()
    p.join()

以下会成功:

import numpy as np
from multiprocessing import Pool, Process

def test(x):
    arr = np.dot(x.T,x) # On large matrices, this calc will use BLAS.

if __name__ == '__main__':
    x = np.random.random(size=((2000,500))) # Random matrix
    p = Process(target = test,args = (x,))
    p.start()
    p.join()
    evaluations = [x for _ in range(5)]
    p = Pool()
    p.map_async(test,evaluations)
    p.close()
    p.join()

【讨论】:

    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 2016-10-21
    • 2017-04-05
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多