Last active
June 1, 2020 14:53
-
-
Save tomekzaw/2948dd41d088aca3662480f803065ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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