python的定时任务模块--schedule
首先先安装一下模块

下面我们简单的学习一下schedule模块
先简单的看个示例
import schedule
def test(*args,**kwargs):
print("hello world 1",datetime.datetime.now())
schedule.every(1).minute.do(test)
while True:
schedule.run_pending()
结果如下,我们可以看到,每隔一分钟执行了一次test这函数

然后我们在看下一个例子
import schedule
import time
def test1(*args,**kwargs):
print("这是test1的函数")
time.sleep(5)
print("这是test1的函数",datetime.datetime.now())
def test2(*args,**kwargs):
print("这是test2的函数")
time.sleep(5)
print("这是test2的函数",datetime.datetime.now())
schedule.every(10).seconds.do(test1)
schedule.every(10).seconds.do(test2)
while True:
schedule.run_pending()
结果如下
这是test2的函数
这是test2的函数 2019-02-11 09:33:55.615493
这是test1的函数
这是test1的函数 2019-02-11 09:34:00.623102
这是test2的函数
这是test2的函数 2019-02-11 09:34:10.638319
这是test1的函数
这是test1的函数 2019-02-11 09:34:15.645928
这是test2的函数
这是test2的函数 2019-02-11 09:34:25.661146
这是test1的函数
这是test1的函数 2019-02-11 09:34:30.668755
这是test2的函数
这是test2的函数 2019-02-11 09:34:40.683972
这是test1的函数
这是test1的函数 2019-02-11 09:34:45.691581
这是test2的函数
这是test2的函数 2019-02-11 09:34:55.706799
这是test1的函数
这是test1的函数 2019-02-11 09:35:00.714407
这是test2的函数
这是test2的函数 2019-02-11 09:35:10.729625
这是test1的函数
这是test1的函数 2019-02-11 09:35:15.737234
这是test2的函数
这是test2的函数 2019-02-11 09:35:25.752451
这是test1的函数
这是test1的函数 2019-02-11 09:35:30.760060
这是test2的函数
这是test2的函数 2019-02-11 09:35:40.775278
这是test1的函数
从结果我们可以很清晰的看到,执行test1和test2两个函数,不是每隔10s执行一次,而是每隔15s执行一次,所以我们可以理解,schedule模块如果去同时执行多个函数的话,这些不同的函数不是开启多线程并行执行的,而是串行执行的,为了解决这个问题,我们可以用到python的多线程模块来解决这个问题
下面我们就通过多线程模块来解决这个问题
import schedule
import threading import time
def test1(*args,**kwargs):
print("这是test1的函数")
time.sleep(5)
print("这是test1的函数",datetime.datetime.now()) def test2(*args,**kwargs):
print("这是test2的函数")
time.sleep(5)
print("这是test2的函数",datetime.datetime.now()) def sch_test1():
threading.Thread(target=test1).start() def sch_test2():
threading.Thread(target=test2).start() schedule.every(10).seconds.do(sch_test1)
schedule.every(10).seconds.do(sch_test2) while True:
schedule.run_pending()
结果如下
这是test1的函数
这是test2的函数
这是test2的函数 2019-02-11 09:42:03.022749
这是test1的函数 2019-02-11 09:42:03.022749
这是test2的函数
这是test1的函数
这是test2的函数 2019-02-11 09:42:13.037967
这是test1的函数 2019-02-11 09:42:13.053567
这是test1的函数
这是test2的函数
这是test2的函数 2019-02-11 09:42:23.053184
这是test1的函数 2019-02-11 09:42:23.068784
这是test1的函数
这是test2的函数
这是test2的函数 2019-02-11 09:42:33.068402
这是test1的函数 2019-02-11 09:42:33.068402
这是test1的函数
这是test2的函数
这是test1的函数 2019-02-11 09:42:43.083620
这是test2的函数 2019-02-11 09:42:43.083620
这是test2的函数
这是test1的函数
这是test2的函数 2019-02-11 09:42:53.098837
这是test1的函数 2019-02-11 09:42:53.114437
这是test2的函数
这是test1的函数
这是test1的函数 2019-02-11 09:43:03.114055
这是test2的函数 2019-02-11 09:43:03.114055
这是test1的函数
这是test2的函数
这是test2的函数 2019-02-11 09:43:13.129272
这是test1的函数 2019-02-11 09:43:13.160472
这是test1的函数
这是test2的函数
这是test1的函数 2019-02-11 09:43:23.144490
这是test2的函数 2019-02-11 09:43:23.144490
从上面的结果我们可以看到,2个函数之间没有干扰了,每隔10s后分别执行了2个函数
python的定时任务模块--schedule的更多相关文章
- python:定时任务模块schedule
1.安装 pip install schedule 2.文档 https://schedule.readthedocs.io/en/stable/faq.html#how-to-execute-job ...
- 定时任务模块 schedule
# coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...
- python实现定时任务
定时任务的实现方式有很多种,如windows服务,借助其他定时器jenkins运行脚本等方式.本文介绍的是python中的一个轻量级模块schedule. 安装 pip命令:pip install s ...
- ansible定时任务模块和用户组模块使用
接上篇,还是一些基础模块的使用,这里主要介绍的是系统模块的使用. 下面例子都进行过相关的实践,从而可以直接进行使用相关的命令. 3.用户模块的使用 用户模块主要用来管理用户账号和用户的属性(对远程主机 ...
- Python源码学习Schedule
关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...
- 【转】Python源码学习Schedule
原文:https://www.cnblogs.com/angrycode/p/11433283.html ----------------------------------------------- ...
- python的库有多少个?python有多少个模块?
这里列举了大概500个左右的库: ! Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...
- python之platform模块
python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
随机推荐
- ---github git clone 加速
https://www.zhihu.com/question/27159393/answer/35528173 git config --global http.postBuffer 52428800 ...
- Linux sed命令使用方法
sed(Stream Editor)是Linux中文本处理使用非常广泛的工具,可以对文件内容进行替换.删除.新增.选取特定行等功能.下面通过sed常用实例介绍sed命令的使用方法. sed基本语法 s ...
- [leetcode]股票题型123
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- javaweb 学习系列【转】
http://www.cnblogs.com/xdp-gacl/category/574705.html jsp指令 http://www.cnblogs.com/huiyuantang/p/5332 ...
- Ajax 学习 第四篇
使用jQuery实现Ajax 跨域
- 计算平面面积和斜面面积-ArcGIS案例学习笔记
计算平面面积和斜面面积-ArcGIS案例学习笔记 联系方式:谢老师,135_4855_4328,xiexiaokui#139.com 数据:实验数据\Chp8\Ex5\demTif.tif 平面面积= ...
- http://sourceforge.net/projects/rtspdirectshow/
如何做一个解析rtsp协议的h264压缩的实时视频流播放器,带保存功能,目前我有rtsp协议的h264压缩后的实时视频流,目前想开发一个客户端,来播放该实时视频流,同时保存为视频文件,目前似乎有方案是 ...
- 一个漂亮的 PlaceHolder
预览: 不知道为什么下面这个窗口中的 JavaScript 代码没有运行-_-||,想看实际效果就把下面的代码保存下来打开看吧. 代码: <!DOCTYPE HTML> <html ...
- Jupter 7个进阶功能
1. 执行shell命令 Shell是一种与计算机进行文本交互的方式. 一般来讲,当你正在使用Python编译器,需要用到命令行工具的时候,要在shell和IDLE之间进行切换. 但是,如果你用的是 ...
- python基础学习Day9 函数的初识,实参、形参、
1.函数 def my_len(): l = [,,,,,,] count = for i in l: count += print(count) my_len() 定义的my_len()方法时,其结 ...