1

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.

Raja
  • 13
  • 2
  • Why would the if condition exit the program on fail? You are just running code when running is True, not stopping the code when running is True. I think you meant to add this to the while loop instead of another if statement. – Ayush Garg Jun 26 '21 at 17:55
  • You could break out from the loop instead of constantly calling an extra if statement. – nagyl Jun 26 '21 at 17:56
  • @megargayu. Yes. I had tried that earlier and then split the conditions to check. But you are right. I have now added the If condition to the while loop so that it exits the loop when the condition fails. Thank you. – Raja Jun 27 '21 at 11:04

1 Answers1

1

When you do running = False in your updaterunning function, it's setting a local variable called running; the variable in the outer scope is not changed.

One way to address this is with globals, but a better solution IMO is to put this state into an object.

class ThingDoer:
    def __init__(self):
        self.running = False

    def updaterunning(self):
        logger.info("running is set")
        self.running = False

    def func_function4(self):
        logger.info("starting function 4")
        self.running = True
        timeout = time.time() +10
        a = 1
        while time.time() <= timeout:
            if self.running == True:
                logger.info("function 4 : %s", a)
                a+=1
                time.sleep(1)
                if a == 7:
                    self.updaterunning()
        logger.info("exiting function 4")

f4 = threading.Thread(target = ThingDoer().func_function4)
f4.start()

By putting this state in a class, you make it possible to have more than one ThingDoer operating in parallel without interfering with each other (which would lead to some very difficult-to-debug behavior), but if you want to only have a single piece of running state, you can also do that by having ThingDoer be a singleton.

Samwise
  • 68,105
  • 3
  • 30
  • 44