您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用numpy进行多处理会使Python在OSX上意外退出

使用numpy进行多处理会使Python在OSX上意外退出

我想出了解决该问题的方法。在初始化多处理实例之前,将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()
python 2022/1/1 18:31:07 有455人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶