Timing task APScheduler, keep data synchronization at all times

Keywords: crontab Python Django Linux

1. What is APScheduler?

2. The usage scenario of APScheduler?

  • When redis persistent storage, APScheduler is used to synchronize data.
  • Users must pay within 30 minutes after placing an order, otherwise cancel the order.

3. What's the difference between APScheduler and crontab as timed task tools?

crontab:

  • crontab is a command provided by the Linux system to accomplish timing tasks.
  • The crontab command provided by Linux is encapsulated with the django-crontab extension
  • It can be independent of the program, does not occupy program resources, and has low coupling.
  • But it's not flexible, like the payment of the above order. crontab doesn't know when to execute it, so it can't.

APScheduler:

  • It can run independently or in programs such as Django and Flask.
  • Flexible, you can start a timed task before the program starts, or you can start a timed task immediately when a task is executed.
  • If you depend on a program, it takes up program resources.

How does APScheduler work?

1. Installation
pip install apscheduler
2 Ways of Use
from apscheduler.schedulers.background import BackgroundScheduler

# Create scheduler objects for timed tasks
scheduler = BackgroundScheduler()

# Define Timing Tasks
def my_job(param1, param2):
    pass

# Add Timing Tasks to Scheduler
scheduler.add_job(my_job, 'date', args=[100, 'python'])

# Start Timing Task Scheduler Work
scheduler.start()
3. Scheduler
  • Using Blocking Scheduler for stand-alone runtime
from apscheduler.schedulers.blocking import BlockingScheduler

  scheduler = BlockingScheduler()
  scheduler.start()  # Here the program will block
  • Use BackgroundScheduler in framework programs such as Django and Flask:
  from apscheduler.schedulers.background import BackgroundScheduler

  scheduler = BackgroundScheduler()
  scheduler.start()  # There will be no blocking in this program.
4. executors can use ThreadPool Executor and Process Pool Executor
  • Thread process pool
from apscheduler.executors.pool import ThreadPoolExecutor
ThreadPoolExecutor(max_workers)  
ThreadPoolExecutor(20) # Up to 20 threads execute simultaneously

Usage method:

executors = {
      'default': ThreadPoolExecutor(20)
  }
  scheduler = BackgroundScheduler(executors=executors)
  • Process pool
from apscheduler.executors.pool import ProcessPoolExecutor
ProcessPoolExecutor(max_workers)
ProcessPoolExecutor(5) # Up to five processes are executed simultaneously

Usage method:

 executors = {
      'default': ProcessPoolExecutor(3)
  }
  scheduler = BackgroundScheduler(executors=executors)
5. Trigger Tigger
  • Scheduling of tasks:

data: Executed at a specific time

interval: Execute at specified intervals

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

cron: Execution on a specified period

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)
t>imezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

  • Example:
 # The code of the function is executed at 0 o'clock every day, and at 0 o'clock, hour can write it without writing it.
App. scheduler. add_job (function name,'cron', hour = 0, args= [parameters that the function needs to pass]) 

# Code execution at 3 a.m. every day
 App. scheduler. add_job (function name,'cron', hour = 3, args= [app])

# If there is no parameter after date, it is to execute the code immediately, which is usually used for testing.
App. scheduler. add_job (function name,'date', args= [app])
6. Program Running:
scheduler.start()
7. Stop APScheduler running
scheduler.shutdown()

Posted by keziah on Thu, 29 Aug 2019 06:26:30 -0700