Skip to content

Instantly share code, notes, and snippets.

@franklindyer
Created July 25, 2023 18:28
Show Gist options
  • Save franklindyer/f101d56aca1b073f88092a6317df1257 to your computer and use it in GitHub Desktop.
Save franklindyer/f101d56aca1b073f88092a6317df1257 to your computer and use it in GitHub Desktop.
"""
A minimal example of using apscheduler
to schedule regularly recurring tasks.
"""
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler
# A sample job
def my_job(msg):
print(msg)
# Modular job-repeating function
def repeat_job(job, delta, sched, args=[]):
job(*args)
now = datetime.now()
run_at = now + delta
sched.add_job(repeat_job, run_date=run_at, args=[job, delta, sched, args])
if __name__ == "__main__":
sched = BlockingScheduler()
repeat_job(my_job, timedelta(seconds=3), sched, args=["Hello with period 3!"])
repeat_job(my_job, timedelta(seconds=7), sched, args=["Hello with period 7!"])
sched.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment