Python timer uses the tutorial apscheduler module, check the folder

1 Introduction


Apscheduler is a task timing module in python, which contains four components: trigger (trigger), job store (job store), executor (executor), scheduler (scheduler)

2. Install


pip install apscheduler

3. Examples

from apscheduler.schedulers.blocking import BlockingScheduler

#作业1
def my_job1():
    print ('hello world!')

#作业2
def my_job2(name):
    print ('hello world,', name)

# 每个五秒运行一次函数
sched = BlockingScheduler()
#不带参数和和带有参数的函数
sched.add_job(my_job1, 'interval', seconds=5)
sched.add_job(func=my_job2, args=('tom',), trigger='interval', seconds=5)
sched.start()

4. Explain


Regarding the trigger (trigger), it has three optional parameters: date / interval / cron.

date: One-time task, that is, the task is executed only once.

The parameters are as follows:

next_run_time (datetime|str) – the date/time to run the job at

timezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

Examples are as follows:

# Execute once after a delay of five seconds

sched.add_job(func=my_job2, args=('tom',), trigger='date', next_run_time=now+datetime.timedelta(seconds=5))

interval: Cyclic task, that is, execute the task according to the time interval.

The parameters are as follows:

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

Examples are as follows:

# Execute the task every five seconds

sched.add_job(func=my_job2, args=('tom',), trigger='interval', seconds=5)

cron: timed task, that is, to execute the task every time period.

The parameters are as follows:

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)

Examples are as follows:

#在1-3,8-10月,每天的下午5点,每一分钟执行一次任务
sched.add_job(func=my_job1, trigger='cron', month='1-3,8-10', day='*', hour='17', minute='*')


from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def aps_test():
    print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '你好'


scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, trigger='cron', second='*/5')
scheduler.start()

common problem

skipped: maximum number of running instances reached (1)

Apscheduler timed task reporting errorskipped: maximum number of running instances reached (1)

The reason is that the default max_instancesmaximum scheduled task is 1, and max_instancesthe number can be increased by adjusting in add_job.
like:

scheduler.add_job(func=add, trigger='cron',minute=3,hour='*/3',misfire_grace_time=3600, max_instances=4)

check folder

        Path("assets/audio").mkdir(parents=True, exist_ok=True)

Guess you like

Origin blog.csdn.net/m0_61634551/article/details/129964863