2

I have 2 daemons, which should access the same Variable. I've created a 3rd file for global variables and each daemon can access the variable. But when one changes the variable the other one still sees the default value.

example:

glob.py

time = 0

daemon a:

import datetime
import time
import glob

while(True):
    glob.time = datetime.datetime.now()
    time.sleep(30)

daemon b:

import glob

while(True):
    print(glob.time)

it would print 0 everytime I hope I've made my problem clear, and someone can help me. If you need some more information please feel free to ask.

3
  • 1
    if your daemons are processes, then they have separate dataspaces. If they are threads, they share the same dataspace (variables). Please clarify how you create/spawn them. Commented Apr 17, 2015 at 14:30
  • 1
    Also, you should avoid naming a file "glob.py" as it will shadow with the stdlib module "glob" - which can lead to unexpected bugs. Commented Apr 17, 2015 at 14:32
  • If you were using threading this would work as expected. So I'm going to assume you are using multiprocessing. See sharing state between processes. Commented Apr 17, 2015 at 14:40

2 Answers 2

4

How do I share global variables across modules?

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.:

import time
import glb

while(True):
    glb.t +=  1
    time.sleep(3)
    print glb.t 

b.py:

import glb
import a
while(True):
    print(glb.t)

glb.py:

t = 0

Output after starting a.py:

python b.py
1
2
3
4
5
6
Sign up to request clarification or add additional context in comments.

3 Comments

Yes - this looks like what the O.P. was trying to do - but it looks like the other modules are being run as independent processes. (I guessed that from he naming them as "daemons")
@jsbueno, based on the title and what the OP's code looks like I will leave the answer. It is the correct way to use a global variable in multiple files.
I 'd not asked for you to remove the answer. It looks like it is not the correct thing the OP is in need - but it might be (and probably will be) the case for other people hitting this question.
2

It looks like (although you don't tell explicitly that) you are running your programs in a completely independent way: two different invocations of the Python interpreter.

There is no such magic as you are hoping would exist: just as if you have two instances of the same program running, each one will have its instance of variables (global or otherwise).

If you are performing some simple task, the easier way to go is to have one text file as output for each process, and the other process trying to read information from the file generated by each process it wants to know about - (you could even use named pipes in Unixes).

The other way is to have a Python script to coordinate the starting of your daemons using the multiprocessing stdlib module, and then create a multiprocessing.Manager object to share variables directly between process. This can be more complicated to set up at first, but it is the clean thing to do. Check the docs on the Manager class here: https://docs.python.org/3/library/multiprocessing.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.