1

The use case here is I have a main program continuously looping to grab sensor data and perform some computations and logic. At the same time I have a second program running flask with twilio listening for inbound sms messages. I am trying to set a variable in the second program based on the content of the sms message, and run logic in the first program depending on the value of this variable. I have defined a module imported into both, and depending on how it is called is supposed to either set the desired value or return it.

To test this I have created a stripped down 'toy' version which just has two independent looping programs and a global module. It is not working the way I expect.

globaltest.py (the global module).

def varex(arg):

    statevar = 0

    if arg != 1:

        statevar = arg

    elif arg == 1:

        return statevar

    else:

        return 'error'

main_test.py (representing the first program)

It is calling to get statevar in globaltest.varex set by stest.

from globaltest import varex
from time import sleep

while True:

    loc_var = varex(1)

    print('local ',loc_var)

    sleep(3)

stest.py (representing the flask server) This is setting statevar in globaltest.varex, in this case just simply indexing upward

from globaltest import varex
from time import sleep

i = 2

while True:

    state = i
    i = i+1

    varex(state)

    print(state)

    sleep(1)

main_test always prints "local 0", so statevar in globaltest.py is not being updated by stest.py .

I can't seem to figure out why this is, and also not sure this is the best way to solve the problem. I've looked at other similar questions but this seems to be a bit of a different use case.

1
  • 1
    They are two different processes on two different systems both importing a module. Just because the module is used in both doesnt mean you can access its variables? Commented May 16, 2018 at 18:42

1 Answer 1

1

"Global" state (which is actually module-level state) is maintained per Python VM. If you want multiple processes to share information then you will need to use some form of IPC or external storage (DBus, mailslots, domain sockets, shared memory, database server, etc.).

Sign up to request clarification or add additional context in comments.

3 Comments

I am running all within the same environment, I think. Both are in the same folder and in each terminal, I am starting a virtual environment before running main_test.py and stest.py . If this is actually starting two different environments I see the problem, but then how do I make sure they are running in the same env?
You can't. One program per VM, one VM per process.
It looks like I was able to solve this simply with SharedArray :-) pypi.org/project/SharedArray/#description

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.