preface

For more, please visit my personal blog.


When it comes to scheduled tasks, the first thing that comes to mind is crontab in Linux, or task scheduling in Windows. These tools are handy to use, but believe it or not, I recently ran into some problems using the crontab command to automate some operations in the messenger flow.

Qsub cannot be submitted using the crontab command, or the administrator has set the user who initiates a crontab task to have no access to the node. In short, the crontab command, which was always convenient, did me a favor. So I had to write my own schedule.

Of course, the core functionality is based on today’s main APScheduler scheduled task framework.

The installation

Installation requires only one command.

pip3 install apscheduler
Copy the code

If you’re not familiar with Python setup and module setup, check out another blog I wrote about Python Setup and module setup.

run

Let’s start with two of the most common schedulers:

  • BlockingScheduler: For scheduler-only programs.
  • BackgroundScheduler: For non-blocking situations, the scheduler runs independently in the background.

Is that what people say? I can read the words, I can’t understand the meaning… Simply put, you can think of BlockingScheduler as single-threaded, and if only scheduled tasks are running in your program, you should choose the BlockingScheduler. The BackgroundScheduler is regarded as multi-threading. If we want to do other calculation in addition to running scheduled tasks in the program, we should choose the BackgroundScheduler.

Here I chose to use BlockingScheduler, where the main program only schedules scheduled tasks and does no other calculations. As follows:

from apscheduler.schedulers.blocking import BlockingScheduler    # import module


def task():
    ' ''Scheduled task'' '
    os.system('python3 spider.py')


if __name__ == '__main__':
    scheduler = BlockingScheduler()

    # add task
    scheduler.add_job(task, 'cron', hour=11, minute=30)

    scheduler.start()
Copy the code

Running the code above will execute the python3 spider.py command at 11:30 a.m. every day.

There’s a new TAB called Cron. It’s called triggers. You can set the conditions for a timed task to trigger.

APScheduler has three built-in triggers:

date

Date: triggers a scheduled task only once on a specific date.

1, execute task function in 2020-1-3
scheduler.add_job(task, 'date', run_date=date(2020, 1, 3))

Execute task function at 1990-12-22 14:30:22
scheduler.add_job(task, 'date', run_date='the 1990-12-22 14:30:22')

# if no time is specified, the command will be executed immediately
scheduler.add_job(task, 'date')
Copy the code

As shown above, the run_date argument can be of type date or STR, or even not explicitly specified.

interval

Interval, after a certain time interval triggers the scheduled task, the interval triggers an infinite number of times.

Execute task at 8:20min 5sec every 3 days a week
scheduler.add_job(task, 'interval', weeks=1,days=3,hours=8,minutes=20,seconds=5)
Copy the code

As shown above, weeks, days, hours, minutes, and seconds are all ints.

cron

Cycle: Triggers a scheduled task within a period. The cycle triggers an infinite number of times.

Execute task every day at 8:20
scheduler.add_job(task, 'cron', hour=8,minute=20)

Execute task once every day from Monday to Friday at 8:20 until the program terminates 2100-05-20
scheduler.add_job(task, 'cron', day_of_week='mon-fri',hour=8,minute=20,end_date='2100-05-20')
Copy the code

The rules for this trigger are similar to crontab. The parameters are described as follows:

parameter instructions
year The value is an int or STR. The value is a four-digit year, such as 2020
month The value is an int or STR, and ranges from 1 to 12 months
week Int or STR. The value ranges from week 1 to 53
day_of_week Int or STR, said which day of the week, can use a scale of 0-6 said can also be expressed in the English abbreviation (mon, tue, wed and thu fri, sat, sun)
day The value is an int or STR, ranging from 1 to 31 days
hour Int or STR. The value ranges from 0 to 23
minute Int or STR. The value ranges from 0 to 59
second Int or STR. The value ranges from 0 to 59 seconds
start_date Datetime or STR, indicating the start time
end_date Datetime or STR, indicating the end time

More programming teaching please pay attention to the public account: Pan Gao accompany you to learn programming