2
I only need to send a command from one process to the other, but I wanted to understand why when modifying the variable within a function, it does not appear in another function.
from multiprocessing import Process
import time
ComandoSerial = 0
class teste():
var = 0
def __init__(self):
self.var2 = 0
def funcao(self):
self.__class__.var = 1
self.var2 = 1
def funcao2(self):
self.__class__.var = 2
self.var2 = 2
def funcao3(self):
self.__class__.var = 3
self.var2 = 3
def retorna(self):
return self.__class__.var
def retorna2(self):
return self.var2
test = teste()
def Teste1():
test.funcao3()
def Teste2():
time.sleep(0.2)
print("VALOR = ", test.retorna())
p = Process(target= Teste1)
p1 = Process(target= Teste2)
def main():
p.start()
p1.start()
p.join()
p1.join()
if __name__ == "__main__":
main()
The problem is that I put the value 3, but reading always returns 0.
The basic problem is that you are using processes and the variables are not shared with each other. You need to share this object somehow. See about it at documentation.
– Woss
It is possible to pass a class instance to a process ?
– HelloWorld
I needed a different "communication" from the documentation. I need it to be according to the code above. In that idea of modifying a value in one process and reading in another.
– HelloWorld