Skip to content
An elegant periodic task executor
Python
Branch: master
Clone or download

Latest commit

sankalp_j
Latest commit 100a3c7 Feb 12, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
timeloop fix module import Jan 23, 2019
.gitignore updated version to 1.0.2 Feb 12, 2019
LICENSE initial commit Sep 18, 2018
MANIFEST.in manifest file added Sep 18, 2018
README.md added image Sep 20, 2018
setup.py updated version to 1.0.2 Feb 12, 2019

README.md

Timeloop

Timeloop is a service that can be used to run periodic tasks after a certain interval.

timeloop

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

Installation

pip install timeloop

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())


@tl.job(interval=timedelta(seconds=10))
def sample_job_every_10s():
    print "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.

You can’t perform that action at this time.