Python kivy clock -- how to get intervals to time from the end of the previous iteration?

Viewed 21

I'm trying to set up Clock.schedule_interval using Kivy in Python.

The relevant code snippet looks something like this:

event = Clock.schedule_interval(lambda x: refresh(),20)

The refresh function usually takes approx 10 seconds to run, but occasionally it will take 60 seconds.

I want the 20 second wait to occur between each iteration of refresh.

That is, I'd like it to work like this:

  1. Refresh. Wait for it to complete (approx 10-60 seconds).
  2. After completion, wait 20 seconds.
  3. Refresh again. Etc.

It seems like the default behavior is to start the 20 second timer when refresh starts, not when it completes. Is there a way to change this?

Also, if refresh is taking 60s and the 20s interval passes, will it start refresh a second time even though refresh is ongoing? Will it queue another thread? If so, is there a way to tell it "If refresh hasn't completed yet, skip this iteration and wait another interval"?

Thanks for any help you can offer!

1 Answers

Use Clock.schedule_once() to run refresh() the first time, then add a Clock.schedule_once() at the end of the refresh() method to schedule itself again in 20 sec.

Related