python用schedule模块实现定时任务

schedule模块用法

 1 import schedule
 2 import time
 3
 4 def test():
 5     print("I‘m working...")
 6 def test2():
 7     print("I‘m working... in job2")
 8
 9 # 每10分钟执行一次job函数
10 schedule.every(10).minutes.do(test)
11 # 每10秒执行一次job函数
12 schedule.every(10).seconds.do(test)
13 # 当every()没参数时默认是1小时/分钟/秒执行一次job函数
14 schedule.every().hour.do(test)
15 schedule.every().day.at("10:30").do(test)
16 schedule.every().monday.do(test)
17 # 具体某一天某个时刻执行一次job函数
18 schedule.every().wednesday.at("13:15").do(test)
19 # 可以同时定时执行多个任务,但是每个任务是按顺序执行
20 schedule.every(10).seconds.do(job2)
21 # 如果job函数有有参数时,这么写
22 schedule.every(10).seconds.do(job,"参数")
23 while True:
24     # 启动服务
25     schedule.run_pending()
26     time.sleep(1)

原文地址:https://www.cnblogs.com/wanglinjie/p/9286323.html

时间: 2024-08-30 17:52:57

python用schedule模块实现定时任务的相关文章

python 面向对象&常用模块(二)

1.面向对象 1.1 异常处理(异常.断言.异常类型.抛出异常.Python异常捕捉) 异常处理 name = "aa" try: # 这一段是要执行的主体 print(name) # except Exception as msg: # 如果没有正常执行,就执行下面的这一段.Exception 是错误类型. except : # 如果没有正常执行,就执行下面的这一段.Exception 是错误类型(有好多类型,可以百度查下,错误类型 ,尽量写的准确些). print("na

python之web模块学习-- urllib

准备写一些列的 python之web模块学习,基本上涉及常用的的web模块,包括 urllib.urllib2.httplib.urlparse.requests,现在,开始我们的第一个模块的学习吧. 1  urllib简介 python urllib 模块提供了一个从指定的URL地址获取网页数据,然后对其进行分析处理,获取我们想要的数据. 2  常用方法 2.1  urlopen  -- 创建一个类文件对象 为读取指定的URL help(urllib.urlopen) urlopen(url,

Python中subprocess 模块 创建并运行一个进程

python的subprocess模块,看到官方声明里说要尽力避免使用shell=True这个参数,于是测试了一下: from subprocess import call import shlex cmd = "cat test.txt; rm test.txt" call(cmd, shell=True) 运行之后: 1:打开并浏览了test.txt文件 2:删除了test.txt文件 from subprocess import call import shlex cmd = &

python时间处理模块 datetime time模块 deltetime模块

1 首先介绍time模块,因为简单 python 自带模块 本人使用time模块,只使用两个函数 time函数和sleep函数 import time a.     time.time()   函数 返回unix时间  常用作两个时间差的计算 b.     time.sleep()  休眠多久,精度为子秒(subsecond) In [90]: t1 = time.time() In [91]: t1 Out[91]: 1461400225.877932 In [92]: time.sleep(

python安装mysqldb模块

今天在阿里云一台新的服务器部署程序后台,发现上面的python缺少MySQLDB 模块,记录安装过程. 首先django程序,运行 python manage.py sycdb 报错: ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb MySQLdb模块的包名字叫mysql-python,于是pip安装之,(关于pip,可以参考这篇文章) 运行: pip install mysql-python

Python 利用pytesser模块识别图像文字

使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the Tesseract engine from Google.是谷歌OCR开源项目的一个模块,可将图片中的文字转换成文本(主要是英文). 1.pytesser安装 使用设备:win8 64位 PyTesser使用Tesseract OCR引擎,将图像转换到可接受的格式,然后执行tesseract提取出文

python学习--创建模块

昨天做了python客户端和服务器端通信,并把接收到的信息写到数据库,因为对数据库进行操作是个经常调用的行为,所以我想把调用数据库的操作写成一个module来给其它python程序调用,所以将昨天的服务器端程序拆分为两个文件: 1.主程序python.py #!/usr/bin/env python import socket import json import connmysql s = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) h

Python中time模块详解

在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平台可能有所不同. UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间.在中国为UTC+8.DST

[ python编程 ] subprocess模块学习总结

转载:http://www.jb51.net/article/48086.htm 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.commands.*不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息. 一.subprocess以及常用的封装函数    运行python的时候,我们都是在创建并