python process

Asked

Viewed 165 times

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.

1 answer

4

A little more complex than you might think at first. This (Sharing state between processes) when it is with integers it is a little simpler than with strings. EX with integers:

from multiprocessing import Process, Value

class Classe():

    def __init__(self, parent=None):
        self.variavel=Value('i', 0)
        self.p = Process(target=self.f, args=())

    def f(self):
        self.variavel.value = 2

    def g(self):
        self.p.start()
        self.p.join()

    def get_var(self):
        return self.variavel.value

Example with strings (I think this is what you want) we have to import c_char_p de ctypes for this case:

from multiprocessing import Process, Value, Manager
from ctypes import c_char_p

class Classe():

    def __init__(self, parent=None):
        manager = Manager()
        self.variavel=manager.Value(c_char_p, "antes")
        self.p = Process(target=self.f, args=())

    def f(self):
        self.variavel.value = "depois"

    def g(self):
        self.p.start()
        self.p.join()

    def get_var(self):
        return self.variavel.value

Use for the two cases presented above:

teste=Classe()
print(teste.get_var())
teste.g()
print(teste.get_var())
  • 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

  • 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.

  • I know I’m testing too

  • Made on top @Megaanim

  • I edited so that Process be called in init, as you had in question

  • 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

  • I edited above. So you have access to the variable in the whole program

  • 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

Show 4 more comments

Browser other questions tagged

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