I am trying to use an object (of class Event) between two processes. The main process creates the object (self.event) and passes it to the new process as args. The new process will call the inject() method of the Event() in foo1() and again the main process will call restore when foo2() is called.
from multiprocessing import Process, Queue
class Event:
def __init__(self):
print('init')
self.i = None
def inject(self):
print(self.i)
self.i = 100
print(self.i)
def restore(self):
print(self.i)
class Test:
def __init__(self):
self.event = Event()
def foo1(self):
p1 = Process(target=self.foo1_target, args=(self.event,))
p1.start()
p1.join()
def foo1_target(self, event):
event.inject()
def foo2(self):
self.event.restore()
t = Test()
t.foo1()
t.foo2()
When I run t.foo1(), it prints
None
100
When I run t.foo2(), it prints
None
My understanding is that t.foo2() should print 100. I am not able to understand why it prints None. Also, how do I use Event object between main process and new process ?