multithreading - Python's multiprocessing Does Not Play Nicely With threading.local? -


I have two processes (see sample code) which is every effort to reach a threading.local object. I hope print the code "a" and "b" (in both order) below. Instead, I get "a" and "a" when I startup whole new processes, then I can do threading. How can I reset the lokal objects beautifully and strong?

  import threading import multiprocessing l = threading.local () lx = 'a' def f (): print getattr (L, 'X', 'B') multi processing. Process (target = f) Start () F ()   

Edit: For reference, when I use threading. Instead of multiprocessing. Process, it works as expected.

The operating system you refer to is UNIX / Linux based and therefore the same Fork () Apply the IIP API. A fork () completely duplicates the process object, as well as its memory, loaded code, open file descriptors and threads. In addition, the new process generally shares the same process object within the kernel until the first memory writing operation. It basically means that local data structures are being copied in the new process, as well as threads local variables. Thus, you still have the same data structures and lx is still defined.

To reset the data structures for the new process, I previously recommended to start the call calling process for some clearing method, for example, you have process_id = os.getpid ( ) can store parent process pid and use

  if process_id! = Os.getpid (): clear_local_data ()   

The main function in the hair process.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -