Python multiprocessing class methods
I'm trying to change a field of a class using its method, but when the method is placed in it, it doesn't work.
from process multiprocessing 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 Mx is still 20. How is this possible?
source to share
First you want to use join
that waits for the process to complete before continuing with the rest of the code.
Second, when you use it multiprocess
, it creates a new instance M
for each Process
. This can be seen when printed self
insideloop
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 this, the value is x
never updated in the original class.
Fortunately, there is a Value object that you can use to do this. This creates a shared memory object that can be modified by 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
source to share
The problem is most likely due to the fact that when you check the value M.x
, it is executed before the start of the loop (due to the parallel nature of multiprocessing). Try putting a print statement inside loop()
and one after p.start()
when printing out M.x
. See which one prints first. It probably is M.x
.
source to share