I have a module with a main function that runs a loop while a flag is true. In the loop, there is a call to a function defined in another module that check a conditions and should stop the main loop if it's True.
The code is:
main.py
import other
isRunning = True
def shutdown():
global isRunning
isRunning = False
def main():
while isRunning:
# some logic here
other.check()
if __name__ == '__main__':
main()
other.py
import main
def check():
someCondition = #some logic to check the condition
if someCondition:
main.shutdown()
The code is run launching the main.py file.
The issue is that when other.check() is called in the loop, the main.shutdown() function is called but the main loop keeps running. In the main loop the isRunning variable is always True, but I was expecting it to become False after being set in the main.shutdown() function.
Why is this happening? What am I missing?
I could refactor the code to mange the exit from the loop in a smarter way, but I am interested to know if there is a solution keeping this kind of code structure.
global isRunningstatement in main().