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 ...
随机推荐
- Jsoup解析网页源码时常用的Element(s)类
Jsoup解析网页源码时常用的Element(s)类 一.简介 该类是Node的直接子类,同样实现了可克隆接口.类声明:public class Element extends Node 它表示由一个 ...
- HTTP 错误 403.6 - Forbidden 解决方案
MSDN 的解决方案 原因 1 Ip 安全的 XML 元素的allowUnlisted属性的值为 false.此外,客户端计算机的 IP 地址不在ip 安全XML 元素之下 IP 地址的列表中.IIS ...
- image 标签src
最近对接到前端 src需要填写 src= "data:image/jpg;base64,xxxxxxxooooooo"; 记录一下图片转换的问题,需要把图片转换成base64 ...
- Linux用户名、用户组的相关命令
whoami 查看当前登录用户 id 用户名 查看用户名的id 及所属组 groups 查看当前登录用户的所有所属组 groups 用户名 查看指定用户的所有所属组 cat /etc/passwd ...
- Haskell语言学习笔记(90)Default
安装 data-default-class $ cabal install data-default-class Installed data-default-class-0.1.2.0 Prelud ...
- 二维码API接口
1.http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.54admin.net 2.http://b.bshare ...
- Python操作远程服务器paramiko模块介绍
paramiko模块是基于Python实现的SSH远程安全连接,可以提供在远程服务器上执行命令.上传文件到服务器或者从指定服务器下载文件的功能. paramiko模块安装方法 paramiko模块不是 ...
- bug提单规范
一.提单模板 标题:[项目组][模块][子模块][发生原因]问题简要描述描述:[预置条件] 有就写清楚,没有就写无[操作步骤]1.XXXXX2.XXXXXX3.XXXXX[实际结果] XXXXX[预期 ...
- Nodejs 菜鸟教程学习-创建第一个应用
注:为了解学习,都是参照http://www.runoob.com/nodejs/nodejs-tutorial.html书写,做下笔记. 对于Nodejs开发来说,在开发一个应用时,我们不仅仅是实现 ...
- javascript 模拟java 实现继承的5种方式
1.继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(t ...