This is due to the way the classes are pickled when sent to the child process. The branded version of the class does not actually contain its internal state, but only the module and class name:
class A: y = 0 pickle.dumps(A) # b'\x80\x03c__main__\nA\nq\x00.'
There is no information about y , this is comparable to a class reference.
The class will crumble in the generated process when it will be passed as argumeht to g , which will import its module (here __main__ ), if necessary, and return a reference to the class, so changes made to it in main will not affect it, since the if __name__ == "__main__" block will not be executed in the subprocess. f directly uses the class in its module, so the effect is basically the same.
The reason x shows different values ββis slightly different. Your f function will output the global variable x from the module. In your main() function, you have another local variable x , so setting x = 1 here will not affect the level of module x in any of the processes. It is passed to g as an argument, so in this case it will have a local value of 1.
source share