I am new to Python and I would like to create what is a 'global static variable', my thread-safe and process-safe queue, between threads/processes created in different modules.
I read from the doc that the concept of a global variable is created using a third module, that I will call as cfg, which defines and initializes my global queue.
I have a problem sharing the instance of this objects between my modules because I tried to print the repr() function over the shared queue imported from the cfg module, inside my other modules that import it, and it shows that they are different instances. It seems that every time I try to import a module a new instance is created and passed to the module who imports it.
Main.py:
import GatewayManager
if __name__ == '__main__':GatewayManager.initialize()doSomething()
GatewayManager.py:
import multiprocessing
import queue
import threadingdef initialize():# Multiprocessing or Threadingglobal isMonoCPUisMonoCPU = multiprocessing.cpu_count() == 1global sharedQueuesharedQueue = multiprocessing.Queue() if not isMonoCPU else queue.Queue()print("gateway: ", sharedQueue.__repr__())
otherModules.py:
import GatewayManager# Some module write on the queue
GatewayManager.sharedQueue.put(variable)# Some read from the queue
GatewayManager.sharedQueue.get()
print("driver: ", GatewayManager.sharedQueue.__repr__())
A multiprocessing.Queue
is shared between the process that creates it (let's call it "Parent") and processes created by Parent (let's call them "Children").
Here is an example of some processes that do not have this relationship:
$ python myprogram.py &
$ python myprogram.py &
The shell is the Parent of these two Children. The shell did not create the multiprocessing.Queue
, though, so it will not be shared by the two children. Instead, they will each create their own. This may be shared with their children but not with each other.
You can easily observe this behavior:
$ cat queuedemo.py
from time import sleep
from os import getpid
from sys import argvfrom multiprocessing import Queueq = Queue()if argv[1:]:q.put(getpid())sleep(60)
else:print(getpid(), q.get())
exarkun@baryon:/tmp/queue$ python queuedemo.py foo & python queuedemo.py
[1] 28249
The second process never manages to read anything from the queue. However, if you give the two process the Parent-Child relationship...
$ cat queuedemo.py
from os import getpidfrom multiprocessing import Queue
from multiprocessing.process import Processq = Queue()
q.put(getpid())def child():print(getpid(), q.get())p = Process(target=child)
p.start()
p.join()
exarkun@baryon:/tmp/queue$ python queuedemo.py
(28469, 28467)
exarkun@baryon:/tmp/queue$
Notice that the q.get()
call succeeds and that the pid put into the queue is different from the pid of the process that gets it out.
Somewhat necessarily, this also extends to processes with Parent-Descendant and Sibling relationships.
So:
- Globals are only shared within a single process
- The multiprocessing module provides tools to share state between processes that are properly related to each other.
If you want to share state between processes without this relationship, there are a variety of other options - and the best one will depend a bit more on what kind of state you have to share and what your sharing patterns look like (neither of which you've included in your question).