I am new to python and I was trying this below code. I have some experience in web development where we have the "setState()" function. Do we have similar functionality in python.
below is my code:
running = True
def updaterunning():
logger.info("running is set")
running = False
def func_function4():
logger.info("starting function 4")
timeout = time.time() +10
a=1
while time.time() <= timeout:
if running == True:
logger.info("function 4 : %s", a)
a+=1
time.sleep(1)
if a == 7:
updaterunning()
logger.info("exiting function 4")
f4 = threading.Thread(target = func_function4)
f4.start()
actual output:
2021-06-26 22:57:01,604 - starting function 4
2021-06-26 22:57:01,609 - function 4 : 1
2021-06-26 22:57:02,623 - function 4 : 2
2021-06-26 22:57:03,637 - function 4 : 3
2021-06-26 22:57:04,652 - function 4 : 4
2021-06-26 22:57:05,663 - function 4 : 5
2021-06-26 22:57:06,675 - function 4 : 6
2021-06-26 22:57:07,686 - running is set
2021-06-26 22:57:07,688 - function 4 : 7
2021-06-26 22:57:08,696 - function 4 : 8
2021-06-26 22:57:09,703 - function 4 : 9
2021-06-26 22:57:10,718 - function 4 : 10
2021-06-26 22:57:11,736 - exiting function 4
expected output:
2021-06-26 22:57:01,604 - starting function 4
2021-06-26 22:57:01,609 - function 4 : 1
2021-06-26 22:57:02,623 - function 4 : 2
2021-06-26 22:57:03,637 - function 4 : 3
2021-06-26 22:57:04,652 - function 4 : 4
2021-06-26 22:57:05,663 - function 4 : 5
2021-06-26 22:57:06,675 - function 4 : 6
2021-06-26 22:57:07,686 - running is set
2021-06-26 22:57:11,736 - exiting function 4
Here, once I set the running to False, I expect the If condition (If running == True) to fail and thus exiting the program. But it keeps on executing. This should be a simple one to fix but I am unaware of the functionality. Thanks for the help.