Exchange of information between Python classes

Asked

Viewed 1,300 times

1

How can I exchange information between two different classes? I need to pass data from one to the other. Each class is running in a different process.

An example of the structure, the code is too big, so here’s an example, the only difference of the real code is what’s inside the classes. I need to communicate to funcao01 and the funcao02, for example.

class Aplicativo():

    def funcao01(self):
        print("teste 01")


class Aplicativo2():
    def funcao02(self):
        print("teste 02")

App = Aplicativo()
App2 = Aplicativo2()

def Telas():
    App.funcao01()

def Comunicacao():
    App2.funcao02()

p = Process(target=Telas)
p1 = Process(target=Comunicacao)



def main():

    p.start()
    p1.start()
    p.join()
    p1.join()



if __name__ == "__main__":
    main()
  • Can [Edit] your question and add code? Are lawsuits distinct or threads distinct?

  • I edited the question!

  • @Andersoncarloswoss you know how to implement these classes?

1 answer

1


A major problem with the above code is that it is not using "classes" - it is declaring functions in the body of a class - the way it is, they nay are "methods", are functions even, who know nothing of the class within which they were declared. This ends up generating an artistic namespace, which works for those who will call these functions from outside, but it is very strange for when these "functions within the class" have to call each other, or share an "attribute": they will have to be prefixed with the class name in full.

The most appropriate way to do this would be to create a method __init__ in the classes, and put the class itself as target of the processes - in this way you will have a "zeroed" instance of each class in each process.

It might be interesting for you to read some documentation and get a better understanding of how this works. This article seems to be a good start (I didn’t read it carefully, but it seems to have the basics) http://pythonclub.com.br/introducao-classes-metodos-python-basico.html

If you find it difficult to use classes and methods in a way that makes sense, remember that Python does not require you to make use of them when your architecture does not need them: you can use simple functions. In your same code example, the functions themselves Telas and Comunicacao have no reason to call another function within the classes - they could perform the task directly.

After you fix this: the way to communicate data between different processes is by using the classes Multiprossecing.Queue and Multiprocessing.Pipe - its use with a small example is documented here: https://docs.python.org/3/library/multiprocessing.html

Your example of code became so minimal that you don’t know what you would like to communicate from one side to the other so any example I created here would be equivalent to what is in the official Python documentation, above.

  • Thanks for the help, already clarified some of the doubts, I’ll give a read on the links you passed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.