An elegant periodic task executor
Find a file
2022-08-05 22:14:33 +02:00
timeloop Fixed job calls 2022-04-28 07:19:27 +02:00
.gitignore updated version to 1.0.2 2019-02-12 17:37:48 +05:30
LICENSE initial commit 2018-09-18 15:10:59 +05:30
MANIFEST.in manifest file added 2018-09-18 16:59:11 +05:30
README.md Fixes grammar, I think? 2022-08-05 22:14:33 +02:00
setup.py updated version to 1.0.2 2019-02-12 17:37:48 +05:30

Timeloop

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

Fork

This fork aims to provide some improvements to the original library. Mainly to be able to set a start time or start offset for tasks.

Installation

Has to be installed manually atm. Since it is just a fork of the original and I still have to set that up and want to respect the work of Sankalp Jonna. Feel free to help tho!

Writing jobs

import time

from timeloop import Timeloop
from datetime import timedelta

tl = Timeloop()

@tl.job(interval=timedelta(seconds=2))
def sample_job_every_2s():
    print "2s job current time : {}".format(time.ctime())

@tl.job(interval=timedelta(seconds=5))
def sample_job_every_5s():
    print "5s job current time : {}".format(time.ctime())


# Added support for initial offset!
@tl.job(interval=timedelta(seconds=10), offset=timedelta(hours=1))
def sample_job_after_an_hour_every_10s():
    print "after an hour 10s job current time : {}".format(time.ctime())

Start time loop in separate thread

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

Start time loop in main thread

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)

Author

  • Sankalp Jonna

Email me with any queries: sankalpjonna@gmail.com.