python 定时器使用教程 apscheduler模块,检查文件夹

22 篇文章 1 订阅
10 篇文章 1 订阅

1.简介


apscheduler是python中的任务定时模块,它包含四个组件:触发器(trigger),作业存储(job store),执行器(executor),调度器(scheduler)

2.安装


pip install apscheduler

3.例子

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.讲解


关于触发器(trigger),它有三种参数可选:date / interval / cron.

date:一次性任务,即只执行一次任务。

参数如下:

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

示例如下:

# 延时五秒后执行一次

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

interval:循环任务,即按照时间间隔执行任务。

参数如下:

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

示例如下:

#每隔五秒执行一次任务

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

cron:定时任务,即在每个时间段执行任务。

参数如下:

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)

示例如下:

#在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()

常见问题

skipped: maximum number of running instances reached (1)

apscheduler定时任务报错skipped: maximum number of running instances reached (1)

原因是默认max_instances最大定时任务是1个,可以通过在add_job中调max_instances增加数量。
如:

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

检查文件夹

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值