Python: Alternating functions every x minutes
Remember that functions are first-class objects in Python. That means you can store them in variables and containers! One way to do it would be:
funcs = [(foo, foo_kill), (bar, bar_kill)]def run(self): counter = self.runTime for sec in range(self.runTime): runner, killer = funcs[counter % 2] # the index alternates between 0 and 1 runner() # do something time.sleep(1800) killer() # kill something counter -= 1
You're overcomplicating this.
while True: foo() time.sleep(1800) foo_kill() bar() time.sleep(1800) bar_kill()
Or if you want to easily add more functions later:
functions = [(foo, foo_kill), (bar, bar_kill), ] # Just append more as neededwhile True: for f, kf in functions: f() time.sleep(1800) kf()
Use a variable to record which function you ran last time. When the timer fires, run the other function and update the variable.