Dorian
Dorian

Reputation: 31

increasing timer in python

I have an infinite for loop that runs like so:

for I in count():
    #doing something
    savetask = savedata()
    if savetask == None:
        timerfunction()


def timerfunction():
     time.sleep(60)

My question or rather what I want to achieve is a function that runs like this. #do something, if savetask == None, sleep for 60 seconds, repeat for loop, if savetask == None AGAIN, sleep for 300 seconds, repeat for loop, if savetask == None AGAIN AGAIN, sleep for 600 seconds.... etc If savetask != None then I want it to restart at 60 seconds or the beginning.

Do I need to pass extra variables so that it knows that it's the second time that the function ran and savetask == None?

Any help or guidance would be appreciated.

Edit:

Because I'm using count() and the count function is:

def count(I=0)
    while not finished():
        yield I
        I += 1

Does that mean that i increases by one each time? So maybe I could use that to count how many times the function runs and reset i if savetask != None?

Upvotes: 0

Views: 492

Answers (1)

James
James

Reputation: 2834

you can exponential backoff doing something like this

for i in count():
    #doing something
    savetask = savedata()
    if savetask == None:
        timerfunction(i)

def timerfunction(i):
     time.sleep(60*2**i)

minor stylistic point, capitals in python are typically for classes. keep variables lower cased

Upvotes: 1

Related Questions