Why can not variables in the same instance be read in different processes? (Python)

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 ButtonObject function represents a click, to modify a value. But the question is, why does the change only occur within function 1 and not of function2, or the opposite if we call the function EventButton inside function2? If both use the same instance of the class, if the value of the variable is modified, should not 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 both 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()
    
asked by anonymous 23.01.2018 / 11:26

2 answers

4

You are not actually accessing the same instance. When you create different processes, the entire program state is duplicated in memory and each process only modifies the instance in the memory that belongs to it.

It is not recommended to use the same memory for separate processes. The ideal is to use a Pipe or Queue:

from multiprocessing import Process, Pipe

def escrever_numeros(conn):
    for i in range(10):
        conn.send(i)

if __name__ == '__main__':
    conn1, conn2 = Pipe()
    p = Process(target=escrever_numeros, args=(conn2,))
    p.start()

    while True:
        print('Recebi do outro processo:', conn1.recv())

In case you really need it, you can use Value and Array .

As recommendations for your code:

  • Do not use __class__ ; you are not editing the instance variables, but rather the class when you do this. To modify instance variables, start them in a __init__ constructor and use self.variavel :

    class Aplicativo(object):
    
        def __init__(self):
            self.comando = 0
    
        def insere_valores(self, cmd):
            self.comando = cmd
    
        ...
    
  • Try using the Python style conventions . The name of your variables, for example, must be in lowercase and separated by _ .

  • For an application with the structure of your example, you are unlikely to really have to use multiprocessing. Consider doing no.

23.01.2018 / 12:12
1

All this is due to the parallelism model used.

In this answer I talked about Java, but the basis applies to most modern operating systems. When you have a new process done, you are doing a new program (via fork to OS). So you will have separate processes.

  

Is there a difference between Program, Thread, and Process?

As seen in the linked response, separate processes have separate memory areas. So, unless you create a IPC , one process will not be able to read the other process variable.

    
23.01.2018 / 12:16