pythonpython-3.xpython-multiprocessing

How to access instance variable in another process


I would like to access an instance variable in another process, the second process which fetches the self.current value returns None.

This is just a sample program a session is created via the assign_value method, so I need to use that session in the check_value method in a second process.

class Check:
    def __init__(self):
        self.current = None

    def assign_value(self):
        print('Assigning value to current')
        self.current = 'hello'

    def check_value(self):
        time.sleep(1)
        print(f'check from process2 {self.current}')


if __name__ == '__main__':
    sam = Check()
    p1 = Process(target=sam.assign_value)
    p1.start()
    p2 = Process(target=sam.check_value)
    p2.start()

OUTPUT:

Assigning value to current
check from process2 None

I tried Queue, multiprocess manager but none helps


Solution

  • Each new process has its own address space. You need to serialise objects to and from the subprocess. Here's an example:

    from multiprocessing import Pool
    
    class Check:
        def __init__(self):
            self._value: str = ""
        @property
        def value(self) -> str:
            return self._value
        @value.setter
        def value(self, _value: str):
            self._value = _value
        def __str__(self) -> str:
            return self.value
    
    def assign(check: Check, value: str) -> Check:
        check.value = value
        return check
    
    if __name__ == "__main__":
        with Pool() as pool:
            check = Check()
            # we have to serialise the Check object as well as the value we want to assign
            check = pool.apply(assign, (check, "Hello world"))
            # the assign() subprocess has serialised the Check object upon return
            # ...so we can access it directly here
            print(check)
    

    Output:

    Hello world