I would like to ask you about multiprocessing for simple calculation in Python

Asked 2 years ago, Updated 2 years ago, 138 views

The code below is a summary (?) of the questions.

Although oversimplified in the code below, the resulting matrix M approached by each process is located in a different position (each process wants to write a value to a different M [row] [column].)

By copying the variable M from the process? The value of the matrix element does not change as the id(M) value is taken differently. I tried to declare it as a global variable, but it didn't work outㅠㅜ I reserved how to use queue because it was too slow.

Processes can take the memory address of the global variable as it is Or is there any other way to get the results...?

Thank you.

from multiprocessing import Process
import numpy
def main():
    mother()
def mother():
    PROCESS_NUM = 4

    M = numpy.array(3) # Variable to write output
    element = [30, 40, 50, 60]

    for i in range(3):
        PROCESS_LIST = []

        for i in range(PROCESS_NUM):
            p = Process(target = pF_mother,  args = (
                    element[i], M,))
            PROCESS_LIST.append(p)
            PROCESS_LIST[i].start()

        for i in range(PROCESS_NUM):
            PROCESS_LIST[i].join()

    return M
def pF_mother(elt, output):
    output = elt

    return output
if __name__ == "__main__":
    main()

python multiprocessing python3

2022-09-22 21:13

1 Answers

You must use Shared memory for multiple processes to communicate with each other.

I'll only give you an example of using Value, which is the simplest example.

'd' after Value means double precision float. If you want to use it as an integer, you can write "i".

In the example below, each of the two processes reads the value written in shared memory, increasing it by 1 and outputting 2.

from multiprocessing import Process, Value, Array

def f(n):
    n.value = n.value + 1

num = Value('d', 0.0)

p1 = Process(target=f, args=(num,))
p2 = Process(target=f, args=(num,))
p1.start()
p2.start()
p1.join()
p2.join()

print(num.value)

For more information on how to use an array other than Value, see here.


2022-09-22 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.