python使用sched模块执行周期性任务和定时任务

执行周期性任务

sched模块是一个通用的事件调度程序,可以对任务进行延迟调度,基于此,可以用它来实现周期性任务。

# coding:utf8
import time
import sched

# 初始化scheduler类
# 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。
s = sched.scheduler(time.time, time.sleep)

# 被周期性调度的任务
def task():
    print("run time: {}".format(int(time.time())))

def perform(inc):
    s.enter(inc, 0, perform, (inc,))
    task()

def main(inc=3):
    s.enter(0, 0, perform, (inc,))
    s.run()

if __name__ == "__main__":
    main()

执行结果为:

>>> run time: 1558716699
>>> run time: 1558716702
>>> run time: 1558716705
>>> run time: 1558716708
>>> ......

任务每隔3s执行一次,周期性执行。

s.enter()方法用来加入调度事件,即将任务加入到队列中,它有四个参数,分别为:间隔时间、优先级(为两个被调度在相同时间执行的函数定序,数字越小,优先级越高)、被调用触发的函数、函数的参数(参数放在元组中,当只有一个参数时,写为(parm,))

s.run()用来执行队列中的任务。

执行定时任务

当单纯执行定时任务时,可以是这样:

# coding:utf8
import time
import sched

s = sched.scheduler(time.time, time.sleep)

def print_time(task_id):
    print("#{} run time: {}".format(task_id, int(time.time())))

def print_some_times():
    print(int(time.time()))
    s.enter(10, 1, print_time, (0,))
    s.enter(5, 2, print_time, (1,))
    s.enter(5, 1, print_time, (2,))
    s.run()
    print(int(time.time()))

if __name__ == "__main__":
    print_some_times()

执行结果为:

>>> 1558716127
>>> #2 run time: 1558716132
>>> #1 run time: 1558716132
>>> #0 run time: 1558716137
>>> 1558716137

可以看到,执行总耗时为10s,执行开始后5s,1号和2号任务同时到达执行时间,由于2号任务的优先级高先执行,后面1号任务执行,10s后,0号任务执行。

原文地址:https://www.cnblogs.com/lucky-heng/p/10921007.html

时间: 2024-08-27 23:33:20

python使用sched模块执行周期性任务和定时任务的相关文章

python的sched模块--延时调度

我们经常需要定时的执行某个任务,在Linux下我们有强大的crontab,但是在Python这个粒度(定时执行函数),如何处理呢?除了第三方的模块外,标准库为我们提供了sched模块和Timer类. 先说sched模块,准确的说,它是一个调度(延时处理机制),每次想要定时执行某任务都必须写入一个调度.使用步骤如下:(1)生成调度器:s = sched.scheduler(time.time,time.sleep)第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞.可以说sc

[Python] 利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要函数: 1. commands.getoutput('shell command') 执行shell命令,返回结果(string类型) >>> commands.getoutput('pwd') '/home/oracle' 2. commands.getstatus('file') 该函数

Python标准库笔记(5) — sched模块

事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接口,它需要外部传入两个参数,timefunc是一个没有参数的返回时间类型数字的函数(常用使用的如time模块里面的time),delayfunc应该是一个需要一个参数来调用.与timefunc的输出兼容.并且作用为延迟多个时间单位的函数(常用的如time模块的sleep). 下面是一个列子: imp

python定时任务-sched模块

通过sched模块可以实现通过自定义时间,自定义函数,自定义优先级来执行函数. 范例一 1 import time 2 import sched 3 4 schedule = sched.scheduler( time.time,time.sleep) 5 6 def func(string1): 7 print "now excuted func is %s"%string1 8 9 print "start" 10 schedule.enter(2,0,func

python中关于不执行if __name__ == '__main__':测试模块的解决

1.新建测试脚本文件: 2.编辑测试脚本 import unittest import requests import json import HTMLTestRunner ur1 = 'http://118.178.247.67:8081/systLogonUser/adminLogon.do' headers = {'Content-Type':'application/x-www-form-urlencoded','Referer':'118.178.247.67'} data = { '

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高手之路【七】python基础之模块

本节大纲 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式 1:模块介绍 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以

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

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

[ 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的时候,我们都是在创建并