代码之家  ›  专栏  ›  技术社区  ›  Mathieu

多次调用时的池行为

  •  0
  • Mathieu  · 技术社区  · 7 年前

    根据我的观察,我有一个非常简单的问题:

    让我们考虑以下代码:

    import multiprocessing as mp
    
    def f(x,y):
        # Do heavy stuff
    
    N = 8
    param_to_test = [(x,y) for x in range(1000) for y in range(1000)]
    with mp.Pool(processes = N) as p:
        p.starmap(f, param_to_test)
    
    param_to_test = [(x,y) for x in range(1000, 2000) for y in range(1000, 2000)]
    with mp.Pool(processes = N) as p:
        p.starmap(f, param_to_test)
    
    param_to_test = [(x,y) for x in range(2000, 3000) for y in range(2000, 3000)]
    with mp.Pool(processes = N) as p:
        p.starmap(f, param_to_test)
    

    我们还假设每个迭代的持续时间是恒定的。我观察到如果我多次打电话 mp.Pool() 在我的主程序中,最后一个比第一个慢得多。

    在我的实际程序中,每次调用 with mp.Pool() ,param_to_test是根据前一个测试的计算结果计算的。 我知道在python中生成过程实际上并不快,而且可能有一种更好的方法来编写代码,而不是生成3次(或更多次)相同的代码 Pool() 工人的。然而,在我的情况下,这段时间可以忽略不计。

    我的问题是: 池() 每一次,是否有任何问题,我不知道的行为 这就可以解释为什么计算速度会逐渐减慢?

    0 回复  |  直到 7 年前