python - 多处理卡住计算机

标签 python windows python-multiprocessing

我通过使用多处理改进了我的执行时间,但我不确定 PC 的行为是否正确,它会卡住系统,直到所有进程完成。 我正在使用 Windows 7 和 Python 2.7。

也许我做错了,这是我做的:

def do_big_calculation(sub_list, b, c):
    # do some calculations here with the sub_list

if __name__ == '__main__':
    list = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
    jobs = []
    for sub_l in list :
        j = multiprocessing.Process(target=do_big_calculation, args=(sub_l, b, c))
        jobs.append(j)
    for j in jobs:
        j.start()

最佳答案

在这里,您为每个任务创建了 1 个 Process。这将并行运行您的所有任务,但它会给您的计算机带来沉重的开销,因为您的调度程序将需要管理许多进程。这可能会导致系统卡住,因为您的程序使用了太多资源。

这里的解决方案可能是使用 multiprocessing.Pool 来运行给定数量的同时执行某些任务的进程:

import multiprocessing as mp

def do_big_calculation(args):
    sub_list, b, c = args
    return 1

if __name__ == '__main__':
    b, c = 1, 1
    ll = [([1, 2, 3, 4], b, c),
          ([5, 6, 7, 8], b, c),
          ([9, 10, 11, 12], b, c)]
    pool = mp.Pool(4)
    result = pool.map(do_big_calculation, ll)
    pool.terminate()
    print(result)

如果你准备使用第三方库,你也可以看看concurrent.futures (你需要在 python2.7 中安装它,但它存在于 python3.4+)或 joblib (可用 pip):

from joblib import Parallel, delayed

def do_big_calculation(sub_list, b, c):
    return 1

if __name__ == '__main__':
    b, c = 1, 1
    ll = [([1, 2, 3, 4], b, c),
          ([5, 6, 7, 8], b, c),
          ([9, 10, 11, 12], b, c)]
    result = Parallel(n_jobs=-1)(
        delayed(do_big_calculation)(l, b, c) for l in ll)
    print(result)

此类库的主要优点是它正在开发,而 python2.7 中的 multiprocessing 已卡住。因此,错误修复和改进相对频繁。
它还实现了一些巧妙的工具来减少计算开销。例如,它对大 numpy 数组使用内存映射(减少启动所有作业的内存占用)。

关于python - 多处理卡住计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39892551/

相关文章:

python - 在返回列表的后台运行函数

python - 数据打印不正确

python - 如何在不先创建窗口的情况下通过 tkFont 测量字符串呈现的宽度?

python - 为什么每个帧的长度不相等?

linux - Windows 管道的 EOL 处理

linux - Docker:从 Win10 安装到运行 ubuntu 的容器的目录中不允许进行 git 操作

c - 从 Windows 到 Linux 的语法错误 Fluent UDF

Python 3.4 多处理不适用于 py2exe

带有 RotatingFileHandler 的 Python 3 记录器超过 maxBytes 限制

python - 如何使用 Python 替换 HTML 转义字符?