0

I have a singleton class which I use. I make an instance of this class on my Main Thread.

I Create a few multi-processes and what seems logical (due to how the GIL works) to me is that when I instantiate the same class there its a new instance, it cannot see the one instantiated at my Main Thread.

But if I do the same on Linux, when creating my multi-processes and instantiate my class it returns the pointer to the instance created at my Main Thread and can thus see the data from that one.

This might be touching on some basic principle of threading in both Linux and Windows I do not understand. But if someone could help me briefly to understand or point to where I can read about it

Regards

EDIT

On Windows:

def child_process_function():
    SingletonThread = instantiate_singleton() <-- This will be a new instance

    do_work()

SingletonMain = instantiate_singleton()
process = multiprocessing.Process(target=child_process_function)
process.start()

On Linux

def child_process_function():
    SingletonThread = instantiate_singleton() <-- This will contain the data from the SingletonMain. 

    do_work()

SingletonMain = instantiate_singleton()
process = multiprocessing.Process(target=child_process_function)
process.start()

Now after reading your link the Linux SingletonThread does not point to the SingletonMain as I thought at first its simply just data copied. However my problem is that because the data does not get copied on windows I send the data through a Queue (The data changes all the time so this is the desired method). However there is a Overwrite block on the class (it contains settings) so if the data is already there as on Linux I get and write error on the class. However adding the.

import multiprocessing as mp
mp.set_start_method('spawn')

Fixes my problem.

Ephreal
  • 1,835
  • 2
  • 18
  • 35

1 Answers1

0

When spawning a new process, UNIX OSes use the fork strategy which literally duplicates the parent process so that the child has the exact copy of the parent memory and resources. The memory is duplicated and not shared. Therefore, changes applied on the child singleton instance won't be seen by the parent and vice-versa.

You are misled by the fact you see the "same pointer" because of how virtual memory is managed in modern OSes. The pointers you see are the same because they start from the same offset but they actually belong to different memory pages. I gave a similar explanation here.

You can read more about process launching mechanisms in the multiprocessing module documentation.

noxdafox
  • 14,439
  • 4
  • 33
  • 45