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.