- New Feature: restart stop and start a job (with timer reset).
- job decorator renamed to add_job
- Can handle only one job. For each add_job call previous job will be overwritten. Use multiple TimeLoop() instances to declare more than one job.
- No logging messages.
Timeloop is a service that can be used to run periodic tasks after a certain interval.
Each job runs on a separate thread and when the service is shut down, it waits till all tasks currently being executed are completed.
Inspired by this blog here
pip install timeloop
import time
from timeloop import Timeloop
from datetime import timedelta
tl = Timeloop()
@tl.add_job(interval=timedelta(seconds=2))
def sample_job_every_2s():
print "2s job current time : {}".format(time.ctime())
By default timeloop starts in a separate thread.
Please do not forget to call tl.stop
before exiting the program, Or else the jobs wont shut down gracefully.
tl.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
tl.stop()
break
Doing this will automatically shut down the jobs gracefully when the program is killed, so no need to call tl.stop
tl.start(block=True)
- Sankalp Jonna
Email me with any queries: [email protected].