timeloop/README.md

43 lines
1 KiB
Markdown
Raw Normal View History

2018-09-18 13:19:06 +02:00
# Timeloop
Timeloop is a service that can be used to run periodic tasks after a certain interval.
2018-09-18 11:40:59 +02:00
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`](https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/)
## Installation
```sh
2018-09-18 13:19:06 +02:00
pip install timeloop
2018-09-18 11:57:32 +02:00
```
2018-09-18 11:40:59 +02:00
## writing a job looks like this
```python
import time
2018-09-18 13:19:06 +02:00
from timeloop import Timeloop
2018-09-18 11:40:59 +02:00
from datetime import timedelta
2018-09-18 13:19:06 +02:00
tl = Timeloop()
2018-09-18 11:40:59 +02:00
2018-09-18 13:19:06 +02:00
@tl.job(interval=timedelta(seconds=2))
2018-09-18 11:40:59 +02:00
def sample_job_every_2s():
print "2s job current time : {}".format(time.ctime())
2018-09-18 13:19:06 +02:00
@tl.job(interval=timedelta(seconds=5))
2018-09-18 11:40:59 +02:00
def sample_job_every_5s():
print "5s job current time : {}".format(time.ctime())
2018-09-18 13:19:06 +02:00
@tl.job(interval=timedelta(seconds=10))
2018-09-18 11:40:59 +02:00
def sample_job_every_10s():
print "10s job current time : {}".format(time.ctime())
2018-09-18 13:19:06 +02:00
tl.start()
2018-09-18 11:40:59 +02:00
```
## Author
* **Sankalp Jonna**
Email me with any queries: [sankalpjonna@gmail.com](sankalpjonna@gmail.com).