I have a big problem with Multi Processing. in this case I have a
1.Main Class in Main Process
2.Foo Class in Another Process
I Have To Change Some Variables Inside of Process2 With Main Process. How Can I Do That/???
class Main:
def __init__(self):
self.Foo_Instance = Foo()
multiprocessing.Process(target=self.Foo_Instance.do_something).start()
def Change_Foo(self):
Foo_Instance.ImportantVar = True
class Foo:
def __init__(self):
self.ImportantVar = False
def do_something(self):
pass
Main_Instance = Main()
Main_Instance.Change_Foo()
Each process in general has its own memory that is inaccessible to any other process. If you want one process to be able to modify a variable that another process is using, then the simplest solution is to create this variable in shared memory. In the demo below we create such a variable using a
multiprocessing.Valueinstance. To demonstrate thatMain.Change_Foocan modifyFoo'sImportantVarattribute, we have to giveFoo.do_somethinga chance to print out its initial value beforeMain.Change_Foomodifies it. Likewise,Foo.do_somethingneeds to wait forMain.Change_Footo change the value before it prints out the updated value. To accomplish this we use two 'multiprocessing.Event' instances:Prints: