为什么我的var值没有改变,也没有传达?

2024-09-28 16:57:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python程序来创建两个数组,将它们相乘,然后将相乘的结果相加。你知道吗

这是通过过程完成的。你知道吗

如果我创建两个进程,程序将创建两个具有两行两列的数组。 它们应该更改varresultado的全局值。你知道吗

但是,进程2错误地接收了varresultado的值,正如您在代码中的注释中看到的那样。你知道吗

可能是什么?你知道吗

程序的某些部分被隐藏了。你知道吗

resultado = []

def multiplicacao(pipe_filho):
    if flag:
         sem.acquire()
    global resultado
    # print resultado --> 
    #   Process 1: [] (Correct) ; 
    #   Process 2: [] (Incorrect should be [[47, 6]])
    myVar = productMatrix(A[e], B)
    resultado += myVar
    # print resultado --> 
    #   Process 1: [[47, 6]] (Correct) ;
    #   Process 2: [[36, 127]] (Correct)
    for i in range(len(myVar)):
        for j in range(len(myVar[i])):
            soma.value += myVar[i][j]
    pipe_filho.send(myVar)
    if flag:
        sem.release()

pipe_filho, pipe_pai = Pipe()

for i in range(2):
    e = i
    newP = Process(target=multiplicacao, args=(pipe_filho,))
    newP.start()

myVar = pipe_pai.recv()

for i in range(2):
    newP.join()

# printMatrix(resultado) --> 
#   | 36  95 | (Incorrect should be | 36  95  36  127 | )

Tags: in程序for进程range数组processpipe