Skip to content

Instantly share code, notes, and snippets.

@jackgolding
Created January 27, 2015 09:04
Show Gist options
  • Save jackgolding/8d35defc53cd18e85627 to your computer and use it in GitHub Desktop.
Save jackgolding/8d35defc53cd18e85627 to your computer and use it in GitHub Desktop.
[APSCHEDULER] cron jobs administrating a scheduled job
from apscheduler.jobstores.base import JobLookupError
from apscheduler.schedulers.background import BackgroundScheduler
import time
def hello():
print(time.localtime().tm_sec)
def kill_hello(scheduler):
try:
scheduler.remove_job('hello_job')
except JobLookupError:
return
def start_hello(scheduler):
scheduler.add_job(hello, 'interval', seconds=2, id='hello_job')
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(kill_hello, 'cron', second=0, id='kill_hello', args=[scheduler])
scheduler.add_job(start_hello, 'cron', second=30, id='start_hello', args=[scheduler])
scheduler.start()
while True:
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment