Skip to content

Instantly share code, notes, and snippets.

@danemacaulay
Last active November 30, 2018 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danemacaulay/0e049fff9f65d95dbd4334d94bc1893c to your computer and use it in GitHub Desktop.
Save danemacaulay/0e049fff9f65d95dbd4334d94bc1893c to your computer and use it in GitHub Desktop.
python background scheduler
import time
import threading
import datetime
class Scheduler(object):
def __init__(self, hours, job):
self.interval = datetime.timedelta(hours=hours).seconds
self.job = job
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
while True:
self.job()
time.sleep(self.interval)
@danemacaulay
Copy link
Author

danemacaulay commented Nov 30, 2018

import datetime

def print_time():
  print(datetime.datetime.now())

Scheduler(hours=1, job=print_time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment