Detail python scheduling framework APScheduler use
- 2020-05-27 06:07:10
- OfStack
Recently, I was on the way to study the use of python scheduling framework APScheduler, so today is also a study note!
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(tick, 'interval', seconds=3) # interval 3 Seconds to perform 1 time
scheduler.start() # The scheduling task here is independent 1 A thread
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) # Other tasks are executed in separate threads
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
Non-blocking schedule, executed once at the specified time
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05') # At the specified time, only execute 1 time
scheduler.start() # The scheduling task here is independent 1 A thread
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) # Other tasks are executed in separate threads
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
Non-blocking mode, which is executed in cron mode
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import time
import os
from apscheduler.schedulers.background import BackgroundScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BackgroundScheduler()
#scheduler.add_job(tick, 'interval', seconds=3)
#scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')
scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')
'''
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)
* 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
'''
scheduler.start() # The scheduling task here is independent 1 A thread
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
# This is here to simulate application activity (which keeps the main thread alive).
while True:
time.sleep(2) # Other tasks are executed in separate threads
print('sleep!')
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
Block mode, execute once every 3 seconds
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
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)
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start() # It's blocking. It's only blocking 1 Threads are dedicated to scheduling tasks
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
Use the blocking method and execute only once
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
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, 'date', run_date='2016-02-14 15:23:05')
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start() # It's blocking. It's only blocking 1 Threads are dedicated to scheduling tasks
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')
Block mode, cron scheduling method is used
# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
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, 'cron', day_of_week='6', second='*/5')
'''
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)
* 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
'''
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start() # It's blocking. It's only blocking 1 Threads are dedicated to scheduling tasks
except (KeyboardInterrupt, SystemExit):
# Not strictly necessary if daemonic mode is enabled but should be done if possible
scheduler.shutdown()
print('Exit The Job!')