python教程:apscheduler模块使用教程
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 attimezone (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:定时任务,即在每个时间段执行任务。
参数如下:
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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='*')
python教程:apscheduler模块使用教程相关推荐
- Python之Bs4模块安装教程
Python安装Bs4模块的方法有多种,我这里主要演示自己实际安装成功的方法. 1.在终端打开powershell,输入执行指令(前提是需要支持pip指令): pip3 install Beautif ...
- 飘逸的python - 简明gzip模块压缩教程
压缩数据创建gzip文件 先看一个略麻烦的做法 import StringIO,gzip content = 'Life is short.I use python' zbuf = StringIO. ...
- Xposed模块开发教程(一) 了解并使用Xposed
系列教程: Xposed模块开发教程(一) 了解并使用Xposed http://blog.csdn.net/u014418171/article/details/52910768 Xposed模块开 ...
- python模块使用_PYthon模块使用教程(最新).doc
您所在位置:网站首页 > 海量文档  > 计算机 > Python PYthon模块使用教程(最新).doc63页 本 ...
- python模块编程教程_python进阶教程之模块(module)介绍
我们之前看到了函数和对象.从本质上来说,它们都是为了更好的组织已经有的程序,以方便重复利用. 模块(module)也是为了同样的目的.在Python中,一个.py文件就构成一个模块.通过模块,你可以调 ...
- python常用模块教程_盘点Python常用的模块和包
模块 1.定义 计算机在开发过程中,代码越写越多,也就越难以维护,所以为了编写可维护的代码,我们会把函数进行分组,放在不同的文件里.在python里,一个.py文件就是一个模块. 2.优点: 提高代码 ...
- python手机安装模块_python如何安装模块 模块安装操作教程
Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言,设计具有很强的可读性,相比其他语言经常使用英文关键字,它具有比其他语言更有特色语法结构.那么Python的安装模块该怎么使用 ...
- Python中Collections模块的Counter容器类使用教程
Python中Collections模块的Counter容器类使用教程 1.collections模块 collections模块自Python2.4版本开始被引入,包含了dict,set,list, ...
- 如何制作并使用python发布的模块压缩包和whl包,并进行pip安装和使用教程,加上传Pypi,上传之后又如何增删whl项目
如何制作并使用python发布的模块压缩包和whl包,并进行pip安装和使用教程 0 对于python中包概念的理解 一 创建setup.py文件 # 打包成模块压缩包 from distutils. ...
最新文章
- automaticallyAdjustsScrollViewInsets
- LiveVideoStack线上分享第五季(七):开源流媒体服务器:为何一定得再撸个新的...
- 自定义标签 (choose)
- mysql php释放内存_php mysqli_free_result()函数释放结果集
- 这些Python代码技巧,你肯定还不知道
- 《Apache Kafka 实战》读书笔记-认识Apache Kafka
- VMware资源集合,分享一波
- python求解LeetCode习题Find the First Missing Positive Number
- C++ std::thread::hardware_concurrency() 获取CPU核心数
- 12306 登录验证数据下载(未标定)
- cesium绘制网格_Cesium (五) 栅格图层
- VBA写入公式(4):数字转大写金额公式
- 中国计算机学会推荐国际学术会议和期刊目录 2015
- 基于web的木子日记个人博客网站的设计与实现
- ffmpeg中文文档
- SSM框架终极篇——Spring、SpringMVC、MyBatis整合练习(超级详细)
- 兰州财经大学计算机网络,兰州财经大学教务网络管理系统入口
- 网页中怎么屏蔽鼠标右键和部分按键功能
- nginx反向代理去除目录层级转发
- 电脑卡的原因和解决方法