5
I’m trying to change an external variable within a process but it’s not working. I created a simple test class:
from multiprocessing import Process
class Classe():
def __init__(self, parent=None):
self.variavel="antes"
self.p = Process(target=self.f, args=(self.variavel,))
def f(self, variavel2):
variavel2="depois"
def g(self):
self.p.start()
self.p.join()
print self.variavel
teste=Classe()
teste.g()
But when performing this, it prints the old value of the variable, someone knows how to access a normal variable within the process, I tried to use self.variable but it did not work either.
But that won’t solve my problem... I need the process to edit the class variable, and it gets the new value, and this is not happening
– Mega Anim
https://s31.postimg.org/jx87yh14r/teste.png Look at the test in the terminal, I put the class h to show the variable after it has been edited by process I even tried to wait and run test. h() varis times later and he still prints before.
– Mega Anim
I know I’m testing too
– Miguel
Made on top @Megaanim
– Miguel
I edited so that
Process
be called in init, as you had in question– Miguel
Look, I don’t think so, because I want to create a process in my software that I am creating, and the variables that I need to edit need to be your right type, string, and such, can not be manager, because they are used throughout the program... I will explain better what I need, I have a function: def functionA(self): I do my function << I need everything executed in this function to be executed in the background in a different process to make the program lighter... That was the reason I decided to look for how to do multiprocessing
– Mega Anim
Let’s go continue this discussion in chat.
– Miguel
I edited above. So you have access to the variable in the whole program
– Miguel
In the background the variable is not manager type, self.variavel.value is int or string in the examples I gave you, self.variable is manager manager
– Miguel