I have two processes (see sample code) that each attempt to access a threading.local object. I would expect the below code to print “a” and “b” (in either order). Instead, I get “a” and “a”. How can I elegantly and robustly reset the threading.local object when I startup whole new processes?
import threading
import multiprocessing
l = threading.local()
l.x = 'a'
def f():
print getattr(l, 'x', 'b')
multiprocessing.Process(target=f).start()
f()
edit: For reference, when I use threading.Thread instead of multiprocessing.Process, it works as expected.
Both operating systems you mentioned are Unix/Linux based and therefore implement the same
fork()ing API.A
fork()completely duplicates the process object, along with its memory, loaded code, open file descriptors and threads. Moreover, the new process usually shares the very same process object within the kernel until the first memory write operation. This basically means that the local data structures are also being copied into the new process, along with the thread local variables. Thus, you still have the same data structures andl.xis still defined.To reset the data structures for the new process, I’d recommend the process starting function to first call for some clearing method. You could, for example, store the parent process pid with
process_id = os.getpid()and useIn the child process main function.