在用的apscheduler库
pip install apscheduler==2.1.2
依赖:six,tzlocal等
最新的库
https://pypi.python.org/simple/apscheduler/
在django 的urls.py下
#定时任务
from apscheduler.scheduler import Scheduler #调用2.1.2版本的库,最新的已舍去
sched = Scheduler()
@sched.interval_schedule(seconds=1) #时间调度任务,间隔1秒
def mytask():
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# checkUserFiles()
@sched.cron_schedule(second='50', day_of_week='*', hour='13',minute='1')#d定时任务
#year:年month:月day:日week:星期day_of_week:我不知道啊hour:时minute:分second:秒
def djangotask():
print('定时任务开始')
调度开始
#sched.start(paused=True)
sched.start()
# 示例代码
from apscheduler.triggers.date import DateTrigger # 使用字符串方式1
scheduler.add_job(date_tick) # 使用字符串方式2
scheduler.add_job(date_tick, 'date') # 使用DateTrigger指定时间运行:
date = DateTrigger(datetime.now()+dt.timedelta(seconds=120))
scheduler.add_job(date_tick, date)123456789
# 示例代码
from apscheduler.triggers.interval import IngervalTrigger # 使用字符串方式
scheduler.add_job(interval_tick,'interval',seconds=4,minutes=2,
start_date=datetime.now()+dt.timedelta(seconds=120),
end_date=datetime.now()+dt.timedelta(seconds=360)) # 使用IntervalTrigger指定时间运行
trigger = IntervalTrigger(seconds=60,
start_date=datetime.now()+dt.timedelta(seconds=60),
end_date=datetime.now() + dt.timedelta(seconds=120))
scheduler.add_job(date_tick, trigger)1234567891011
指定具体值,或者每分钟,或者每几分钟,或者每周几等当时 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)123456789101112
Expression | Field | Description |
---|---|---|
* | any | Fire on every value |
*/a | any | Fire every a values, starting from the minimum |
a-b | any | Fire on any value within the a-b range (a must be smaller than b) |
a-b/c | any | Fire every c values within the a-b range |
xth y | day | Fire on the x-th occurrence of weekday y within the month |
last x | day | Fire on the last occurrence of weekday x within the month |
last | day | Fire on the last day within the month |
x,y,z | any | Fire on any matching expression; can combine any number of any of the above expressions |
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有