Singleton python for multiprocessing

How can I use code to share the same instance of the "singletonic" class among processes?

+4
source share
4 answers

It is best to designate one specific process as owning and dedicated to this instance; any other process that requires access to this instance receives it by sending messages to the ownership process through the queue (as provided by the multiprocessing module) or other IPC mechanisms for sending messages, and receives responses through similar mechanisms.

+5
source

The whole point of processes is to have different address spaces. If you want to exchange information between processes, you must use some means of interprocess communication .

+5
source

In Python 2.6, the multiprocessing module has a Value object used for sharing status between processes . They have sample code that should give you an idea of ​​how to exchange data this way, and you can use this approach when writing a singleton class.

+2
source

I don't think you can share an instance between processes, but you can access the shared memory of the instance: http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes in control it if this is really what you want to do.

However, as pointed out in other answers, it may be easier to achieve what you want with the queue.

+1
source

All Articles