I am trying to make functions in different files of a larger program send a message to each other. There is a function without a return statement. In a test example, if I do the following in one file, I can change a global variable and detect its change at run time:
one_file.py
import time
import threading
has_run = False
def processing_function():
global has_run
time.sleep(5)
has_run = True
start = time.clock()
thread = threading.Thread(target=processing_function)
thread.start()
while True:
print(has_run)
time.sleep(0.5)
if (10/3600) < time.clock() - start:
break
When run, this will print False for a while then start printing True.
I tried to get this to work across two files like this:
file1.py
import time
has_run = False
def processing_function():
global has_run
time.sleep(5)
has_run = True
file2.py
from file1 import processing_function, has_run
import time
import threading
start = time.clock()
thread = threading.Thread(target=processing_function)
thread.start()
while True:
print(has_run)
time.sleep(0.5)
if (10/3600) < time.clock() - start:
break
If you now run file2.py, it only prints False a lot of times.
Why is this imported global variable not being modified by the running process, and how can this be refactored so it works?