Created
February 19, 2014 18:13
-
-
Save b1naryth1ef/9097940 to your computer and use it in GitHub Desktop.
Python Scheduler in 22 lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from schedule import schedule, run | |
@schedule(hours=1) | |
def my_task(): | |
print "Yolo Swag!" | |
run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dateutil.relativedelta import relativedelta | |
from datetime import datetime | |
tasks = {} | |
def schedule(**kwargs): | |
def deco(f): | |
tasks[f.__name__] = (datetime.utcnow(), relativedelta(**kwargs), f) | |
return f | |
return deco | |
def run(): | |
while True: | |
time.sleep(1) | |
for name, timeframe in tasks.items(): | |
last, gen, task = timeframe | |
if datetime.utcnow() > last+gen: | |
print "Running task %s" % name | |
task() | |
tasks[name] = (datetime.utcnow(), gen, task) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment