Python frame timing of the task APScheduler

1. Install

pip install apscheduler

2. Four Components

  • Triggers triggers: trigger conditions are set for the task
  • Task memory job store: used to store the task, the task is stored in memory or database
  • Actuator executors: to perform a task, you can set execution mode is single-threaded or thread pool
  • The scheduler schedules: parameters as the above three components, the scheduler operates by creating instances

3. Scheduler

  • BlockingScheduler blocking scheduler: only applies to programs run scheduler.
  • BackgroundScheduler background scheduler: For non-blocking case, the scheduler will run independently in the background.
  • AsyncIOScheduler AsyncIO scheduler for application in the case of using AsnycIO.
  • GeventScheduler Gevent scheduler for use by the GEvent situation.
  • TornadoScheduler Tornado scheduler for Tornado build applications.
  • TwistedScheduler Twisted scheduler for Twisted build applications.
  • Scheduler QtScheduler Qt, Qt use in building applications.

4. Trigger

  • date Date: trigger specific date for a task to run
  • interval interval: time-triggered task run interval
  • cron Period: Period triggered task to run
  • calendarinterval: When you want to run the task interval in a specific time of day to calendar-based use, a task can trigger a variety of settings, for example, can be set to trigger at the same time meet all the conditions to trigger, or meet one that is triggered.

5. Examples

5.1 trigger date (time to perform a certain point)

Code Reading: The program has been in a blocked state, in 2020-01-19 19:08:00the implementation of a continue after blocking

from apscheduler.schedulers.blocking import BlockingScheduler


def spider():
    print('spider name')


schedule = BlockingScheduler()
schedule.add_job(spider, 'date', run_date='2020-01-19 19:08:00')
schedule.start()
5.2 Trigger interval: periodic task
parameter Explanation
weeks(int) Weeks interval
days(int) A few days interval
hours(int) Interval hours
minutes(int) Intervals ranging from minutes
seconds(int) How many seconds interval
start_date(datetime or str) start date
end_date(datetime or str) End Date
timezone(datetime.tzinfo or str) Time zone

Code Interpretation: program has been in a blocked state, 1s performed once in every period of the start_date and end_date; wherein optional start_date and end_date

from apscheduler.schedulers.blocking import BlockingScheduler


def spider():
    print('spider name')


schedule = BlockingScheduler()
schedule.add_job(spider, 'interval', seconds=1, start_date="2020-01-19 19:15:00", end_date="2020-01-19 19:15:20")
schedule.start()
Published 288 original articles · won praise 50 · views 10000 +

Guess you like

Origin blog.csdn.net/gklcsdn/article/details/104044149