Is it possible to have a variable loop @every?
I would like to do something like this:
intervalposition = 1 intervals=[0,1,3,5,10,20,30,60] @every(seconds=intervals[intervalposition]) def main(): #do stuff
And then I can change the
intervalpositionwhen a button is pressed.
Or will I have to do some wait in the
seconds=intervals[intervalposition]works on startup, but it does not change when the value get changed during runtime.
I used a
time.sleep(intervals[intervalposition])at the end of the loop, but that simply makes the whole program wait, so the buttons don't react.
It's possible! Here's what I would do:
import time intervalposition = 1 intervals = [0,1,3,5,10,20,30,60] last_action_time = 0 def action(): # here is your action code! pass @every(seconds=1.0/30) def main(): action_interval = intervals[intervalposition] next_action_fire_time = last_action_time + action_interval if time.time() > next_action_fire_time: action() last_action_time = time.time()
So here you're running the
main()function all the time, but it is checking if it's the right time to call
action(), which is where your code will go. Since
action_intervalis being checked continuously, you can change your interval position and it'll react immediately.
I indeed figured something out like that.
But it would be cool if we could change the interval of an @every