【问题标题】:Process goes in infinite wait, if increasing number of processes如果进程数量增加,进程将无限等待
【发布时间】:2018-05-29 12:59:29
【问题描述】:

我对 python 多处理完全陌生。我正在尝试在我的代码中实现该过程。该代码在有限数量的进程中运行良好。但是当我增加进程数时,进程就会进入无限循环。

基本上,我正在尝试并行化 square 函数,并将值存储在数组中。这些函数通过队列返回值。

from multiprocessing import Process,Queue
import time
import numpy as np
import os


def print_square(num,pos,a,que1):
    """
    function to print square of given num
    """
    a[pos] = num * num

    que1.put((pos,a))

    return




if __name__ == "__main__":

    # number of process
    NP=int(100)

    # creating thread
    p=[]
    num = 2

    a = np.zeros(NP)
    que1 = Queue()
    for i in range (NP):

        p1 = Process(target=print_square, args=(num,i,a,que1))
        # q.append(que1)
        num = num + 1
        p.append(p1)


    print('process created')

    start_time=time.time()
    # starting processes

    for process in p:
        print('cr - i', process)
        process.start()

    print('procesS started')

    for process in p:
        print('join',process,time.time()-start_time)
        process.join()
        process.terminate()

    result = []
    print('result started')
    for process in p:
        print(process)
        result.append(que1.get(block=False))
    print('result completed')
    # result.sort()

    result1 = [r[1] for r in result]

    for i in range(len(result1)):
        a[i] = result1[i][i]


    a1 = time.time()-start_time

【问题讨论】:

    标签: python-3.x parallel-processing queue python-multiprocessing


    【解决方案1】:

    您可能遇到了机器/操作系统限制。例如,请参阅here。你的机器上有多少个内核?我怀疑它是否接近100,因此您不太可能需要生成那么多进程。

    【讨论】:

    • 我有 24 个处理器。我需要产生那么多进程。但是,我发现了问题所在。实际问题是:我在获得队列之前加入了进程,这导致了问题并将其置于死锁中。感谢您的回复。谢谢..!!
    猜你喜欢
    • 2016-07-22
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多