Skip to content

Instantly share code, notes, and snippets.

@tomekzaw
Last active June 1, 2020 14:53
Show Gist options
  • Save tomekzaw/2948dd41d088aca3662480f803065ccc to your computer and use it in GitHub Desktop.
Save tomekzaw/2948dd41d088aca3662480f803065ccc to your computer and use it in GitHub Desktop.
from time import time, sleep
from itertools import count
def tick(seconds, n=None, timefunc=time.time):
start = timefunc()
yield start
gen = count(1) if n is None else range(1, n)
for i in gen:
while True:
now = timefunc()
if now-start >= i*seconds:
yield now
break
sleep((start+i*seconds-now)*0.5) # adaptive waiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment