4.1 Advanced Python Scheduler
Key Points
- Master Advanced Python Scheduler (APS)
Pre-Class Preparation
Installing Apscheduler
Enter the following command in cmd (Windows) or terminal (Mac):
pip install apscheduler
Course Content
What Is APScheduler
APScheduler is a Python-based job scheduling framework that is built on top of Quartz and offers all of its functionality while being extremely user-friendly. It allows for scheduling jobs based on specific dates, fixed time intervals, or CRON expressions, and it also supports job persistence. With these features, building a Python-based job scheduling system becomes a breeze.
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(<Function name>, <Trigger>, The scheduling method for the trigger, )
#scheduler.add_job(<Function name>, <Trigger>, args=(a,) , The scheduling method for the trigger d , )
scheduler.start()
APScheduler offers three main types of triggers: DateTrigger, IntervalTrigger, and CronTrigger. DateTrigger is used to schedule a one-time job on a specific date and time, IntervalTrigger enables looping jobs that run at a fixed interval, and CronTrigger is used for scheduling jobs based on CRON expressions, allowing for more complex and flexible scheduling options.
1. One-time job – DateTrigger
Click to read the docs.
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler
def tick(x):
print('Tick! The time is: %s' % datetime.now(), x)
if __name__ == '__main__':
scheduler = BlockingScheduler(timezone='Asia/Tokyo')
scheduler.add_job(tick, 'date', args=('One-time job',),
run_date=datetime.now() + timedelta(seconds=12))
scheduler.start()
run_date(datetime|str) – the date/time to run the job at
- datetime.date(2009, 11, 6)
- datetime datetime.date(2009, 11, 6)
- ‘2019-11-06 16:30:05’
timezone(datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already
- By default, the local time zone is adopted.
- You can use pytz to specify the time zone. For example, timezone=pytz.utc.
2. Looping job – IntervalTrigger
Click to read the docs.
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'interval', seconds=3)
scheduler.start()
weeks(int) – number of weeks to wait
days(int) – number of days to wait
hours(int) – number of hours to wait
minutes(int) – number of minutes to wait
seconds(int) – number of seconds to wait
start_date(datetime|str) – starting point for the interval calculation
end_date(datetime|str) – latest possible date/time to trigger on
timezone(datetime.tzinfo|str) – time zone to use for the date/time calculations
3. Timed job – CronTrigger
Click to read the docs.
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BlockingScheduler()
#At 17:20:10 every Monday through Sunday
#scheduler.add_job(tick, 'cron', day_of_week='*' ,hour='17', minute='20', second='10')
#At 17:20:10 every day.
#scheduler.add_job(tick, 'cron', hour='17', minute='20', second='10')
#At 17:20:10 during the first week of every year
#scheduler.add_job(tick, 'cron', week='1' ,hour='17', minute='20', second='10')
#At 17:20:10 during the first day of every month
#scheduler.add_job(tick, 'cron', day='1' ,hour='17', minute='20', second='10')
#Executed at 17:20:00 through 17:20:59 during the first day of every month
#scheduler.add_job(tick, 'cron', day='1' ,hour='17', minute='20', second='*')
scheduler.start()
year(int|str) – 4-digit year
month(int|str) – month (1-12)
day(int|str) – day of the (1-31)
week(int|str) – ISO week (1-53)
day_of_week(int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
hour(int|str) – hour (0-23)
minute(int|str) – minute (0-59)
second(int|str) – second (0-59)
start_date(datetime|str) – earliest possible date/time to trigger on (inclusive)
end_date(datetime|str) – latest possible date/time to trigger on (inclusive)
timezone(datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)
Expression | Field | Description |
* | any | Fire on every value |
* /a | any | Fire every a values, starting from the minimum |
a-b | any | Fire on any value within the a-b range (a must be smaller than b) |
a-b/c | any | Fire every c values within the a-b range |
xth y | day | Fire on the x -th occurrence of weekday y within the month |
last x | day | Fire on the last occurrence of weekday x within the month |
last | day | Fire on the last day within the month |
x,y,z | any | Fire on any matching expression; can combine any number of any of the above expressions |