Skip to content

Instantly share code, notes, and snippets.

@r1cebank
Created August 13, 2018 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r1cebank/6120b37ef888012153be01f3e0b481b5 to your computer and use it in GitHub Desktop.
Save r1cebank/6120b37ef888012153be01f3e0b481b5 to your computer and use it in GitHub Desktop.
python interval
import time, threading
StartTime=time.time()
def action() :
print('action ! -> time : {:.1f}s'.format(time.time()-StartTime))
class setInterval :
def __init__(self,interval,action) :
self.interval=interval
self.action=action
self.stopEvent=threading.Event()
thread=threading.Thread(target=self.__setInterval)
thread.start()
def __setInterval(self) :
nextTime=time.time()+self.interval
while not self.stopEvent.wait(nextTime-time.time()) :
nextTime+=self.interval
self.action()
def cancel(self) :
self.stopEvent.set()
# start action every 0.6s
inter=setInterval(0.6,action)
print('just after setInterval -> time : {:.1f}s'.format(time.time()-StartTime))
# will stop interval in 5s
t=threading.Timer(5,inter.cancel)
t.start()
# source: https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval/48709380#48709380
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment