Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing class methods

I'm trying to change a class field using it's method, but when the method is put into it doesn't work.

from multiprocessing import Process

class Multi:
    def __init__(self):
        self.x = 20


    def loop(self,):
        for i in range(1,100):
            self.x = i

M = Multi()

p = Process(target=M.loop)
p.start()

After running this program M.x is still 20. How is that possible?

like image 830
Barty Avatar asked Jul 25 '17 18:07

Barty


People also ask

How many processes should be running Python multiprocessing?

If we are using the context manager to create the process pool so that it is automatically shutdown, then you can configure the number of processes in the same manner. The number of workers must be less than or equal to 61 if Windows is your operating system.

How do you pass multiple arguments in multiprocessing Python?

Use Pool. The multiprocessing pool starmap() function will call the target function with multiple arguments. As such it can be used instead of the map() function. This is probably the preferred approach for executing a target function in the multiprocessing pool that takes multiple arguments.

How does Python multiprocess work?

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.


1 Answers

First, you want to use join, which waits for the process to finish before continuing through the rest of the code.

Second, when you use multiprocess, it creates a new instance of M for every Process. You can see this when printing self within loop

class Multi:
    def __init__(self):
        self.x = 20

    def loop(self):
        print(self, 'loop')
        for i in range(1, 100):
            self.x = i


if __name__ == '__main__':
    M = Multi()
    print(M, 'main 1')
    p = Process(target=M.loop)
    p.start()
    p.join()
    print(M, 'main 2')

>>> <__main__.Multi object at 0x000001E19015CCC0> main 1
>>> <__mp_main__.Multi object at 0x00000246B3614E10> loop
>>> <__main__.Multi object at 0x000001E19015CCC0> main 2

Because of that, the value of x is never updated in the original class.

Fortunately, there is a Value object that you can use that handles this. This creates a shared memory object that can be modified through the processes

from multiprocessing import Process, Value


class Multi:
    def __init__(self):
        self.x = Value('i', 20)

    def loop(self):
        for i in range(1, 100):
            self.x.value = i


if __name__ == '__main__':
    M = Multi()
    p = Process(target=M.loop)
    p.start()
    p.join()

    print(int(M.x.value))
    >> 99
like image 192
Wondercricket Avatar answered Sep 20 '22 14:09

Wondercricket