python timing task trigger (timing model)

APScheduler:
The four components of APScheduler are:

  • Scheduler, trigger, job store, executor

Install command:

pip install setuptools
pip install --ignore-installed apscheduler

1 New scheduler schedulers

BlockingScheduler: The scheduler runs in the main thread of the current process, that is, it will block the current thread
BackgroundScheduler: The scheduler runs in the background thread and will not block the current thread

import datetime as dt
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()

2 Add scheduling task trigger

① date trigger: (trigger at specified time point), the parameters are as follows:

  • run_date ( datetime or str): the date or time the task was run
  • timezone ( datetime.tzinfo or str ): specifies the time zone
# 例1:在 2020-9-24 时刻运行一次 func 方法
scheduler.add_job(func, 'date', run_date = dt.date(2020, 9, 24))
# 例2: 在 2020-9-24 15:10:00 时刻运行一次 func 方法
scheduler.add_job(func, 'date', run_date = dt.datetime(2020, 9, 24, 15, 10, 0))
# 例3: 在 2020-9-24 15:11:00 时刻运行一次 func 方法
scheduler.add_job(func, 'date', run_date = '2020-9-24 15:11:00')

interval trigger: (fixed time interval trigger), the parameters are as follows:

  •  weeks(int): interval of weeks
  • days(int): interval of days
  • hours(int): interval of hours
  • minutes(int): interval of minutes
  • seconds(int): the interval in seconds
  • start_date (datetime or str): start time
  • end_date (datetime or str): end time
  • timezone (datetime.tzinfo or str): time zone
# 例1:每隔两分钟执行一次 func 方法
scheduler.add_job(func, 'interval', minutes = 2)
# 例2:在 2020-9-24 15:15:00 ~ 2020-9-24 15:20:00 之间, 每隔两分钟执行一次 func 方法
scheduler.add_job(func, 'interval', minutes = 2, start_date = '2020-9-24 15:15:00' , 
                  end_date = '2020-9-24 15:20:00')

③ cron trigger: ( triggered periodically at the specified time ), the parameters are as follows:

  • year (int or str): year
  • month (int or str): month
  • day (int or str): day
  • week (int or str): week (1-53)
  • day_of_week (int or str): day of the week (0-6)
  • hour (int or str): hour
  • minute(int or str): minutes
  • second (int or str): seconds
  • start_date (datetime or str): Earliest start time (inclusive)
  • end_date (datetime or str): the latest end time (inclusive)
  •  timezone (datetime.tzinfo or str): specify the time zone

character:

  • 1. * every (every cent) 
  • 2. ? means don't care, arbitrary 
  • 3. - Range (Hours: 1-12, run from 1 to 12 o'clock) 
  • 4. , mark multiple values ​​(hours 1, 2, 3 run at 1 o'clock, 2 o'clock and 3 o'clock) 
  • 5. / Incremental trigger (0/15, run every 15 seconds from 0) 
  • 6. L last (day L, the last day of the month, week L Saturday) 
  • 7. W The nearest working day of the specified date (Monday to Friday) 
  • 8. # serial number (indicating the first few weeks of each month) 
# 例:在每年 1-3、7-9 月份中的每个星期一、二中的 00:00, 01:00, 02:00 和 03:00 执行 func 任务
scheduler.add_job(func, 'cron', month = '1-3,7-9',day='0, tue', hour='0-3')

3 Run the scheduled task

scheduler.start()

3.1 Test time

def forecast_adjust():
    now_temp = datetime.now()
    print('执行方案一', now_temp, '时间间隔: ', now_temp-t0)

def for2():
    now_temp = datetime.now()
    print('执行方案二', now_temp, '时间间隔: ', now_temp-t0)

def fortime3():
    now_temp = datetime.now()
    print('执行方案三', now_temp, '时间间隔: ', now_temp-t0)
    return '9999999999999'
    
def a__():
    b = scheduler.add_job(fortime3, 'cron', hour='15', minute = '18')
    c = scheduler.add_job(fortime3, 'cron', hour='15', minute = '30')
    d = scheduler.add_job(fortime3, 'cron', hour='15', minute = '45')
    print(b)
    print(c)
    print(c)
    return 'kkkqq'
    
t0 = datetime.now()
scheduler = BlockingScheduler()  # 采用阻塞的方式
scheduler.add_job(func=forecast_adjust,
                  trigger=CronTrigger(minute="*/1", second=20,
                                      timezone=tz_now), args=[])

scheduler.add_job(func=for2,
                  trigger=CronTrigger(minute="*/5", second=10,
                                      timezone=tz_now), args=[])

k = a__()
print(k)
scheduler.start()

 

4 features, other operations

APScheduler fixed point, timing:

The four components are: trigger (trigger), job store (job store), executor (executor), scheduler (scheduler)

(1) job stores: management of scheduling tasks:
    ① Add job:

# add_job():可以改变或者移除 job
scheduler.add_job(func, 'interval', minutes = 2)

# scheduled_job():只适用于应用运行期间不会改变的 job
scheduler.scheduled_job(func, 'interval', minutes = 2)

  ②Remove job:

# remove_job() :根据 job 的 id 来移除,所以要在 job 创建的时候指定一个 id
scheduler.add_job(func, 'interval', minutes = 2, id = 'job_one')
scheduler.remove_job(job_one)

# job.remove() :对 job 执行 remove 方法
job = add_job(func, 'interval', minutes = 2, id = 'job_one')
job.remvoe()

     ③ Pause job:

apscheduler.job.Job.pause()
apscheduler.schedulers.base.BaseScheduler.pause_job()

      ④ Resume job:

apscheduler.job.Job.resume()
apscheduler.schedulers.base.BaseScheduler.resume_job()

    ⑤ Modify the job:

# modify_job()
scheduler.modify_job('job_one', minutes = 5)

# job.modify()
job = scheduler.add_job(func, 'interval', minutes = 2)
job.modify(minutes = 5)

    ⑥ Close the job:

scheduler.shutdown()
scheduler.shutdown(wait=false)

(2) Executors: Modules that execute scheduling tasks. There are two commonly used executors:
    ProcessPoolExecutor
    ThreadPoolExecutor

Guess you like

Origin blog.csdn.net/March_A/article/details/129826330