0
I have two processes going on and I have a class containing a method that modifies a value, as you can see in the code below. The Eventoboton function represents a click to modify a value. But the question is, why does the change only occur within function1 and not function2, or the opposite if it is called the Eventoboton function within function2? If the two use the same instance of the class, if the value of the variable is modified, shouldn’t the value 100 be printed on the two calls to show the value? If someone can explain why this does not happen and what would be the way to "access" the value of the variable in the two processes.
#!/usr/bin/python3
from multiprocessing import Process, Queue
class Aplicativo(object):
Comando = 0
def MostraValor(self):
print("VALOR CMD = ", self.__class__.Comando)
def InsereValores(self, Cmd):
self.__class__.Comando = Cmd
def EventoBotao(self):
self.InsereValores(100)
App = Aplicativo()
def Funcao1():
App.EventoBotao()
App.MostraValor()
def Funcao2():
App.MostraValor()
if __name__ == "__main__":
p1 = Process(target=Funcao1)
p2 = Process(target=Funcao2)
p1.start()
p2.start()
p1.join()
p2.join()
Thank you for your reply! I actually have a serial communication in one process and graphical interface running in another. That’s why I summarized to ask the question. I’ve used Queue and everything, but I was seeming insufficient for my needs. That’s why I wanted to modify and read the variable of the "same" instance.
– HelloWorld
@Filipesantos modify variables of the same instance in different processes is not recommended because of synchronization problems; the two processes can write at the same time and this ends up causing more headache than rethinking the structure of the program to be able to use a Queue or Pipe properly. If you want to try, see the documentation that Inkei for
Value
andArray
, but what I would recommend is to invest more time in Queue.– Pedro von Hertwig Batista
Good answer. I think it is worth mentioning that thus: https:/repl.it/repls/InfamousGraveAmurstarfish can solve the problem, because the object on which the processes will work already goes with the modified internal values. These only become 'independent' after each process gets a 'copy' of obj, which in this case already goes with the value
100
– Miguel