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:
>>> run time:
>>> run time:
>>> run time:
>>> ......
任务每隔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()
执行结果为:
>>>
>>> # run time:
>>> # run time:
>>> # run time:
>>>
可以看到,执行总耗时为10s,执行开始后5s,1号和2号任务同时到达执行时间,由于2号任务的优先级高先执行,后面1号任务执行,10s后,0号任务执行。
python使用sched模块执行周期性任务和定时任务的更多相关文章
- python 利用python的subprocess模块执行外部命令,获取返回值
有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...
- Python的sched模块
我们经常需要定时的执行某个任务,在Linux下我们有强大的crontab,但是在Python这个粒度(定时执行函数),如何处理呢?除了第三方的模块外,标准库为我们提供了sched模块和Timer类. ...
- python的subprocess模块执行shell命令
subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...
- [Python] 利用commands模块执行Linux shell命令
http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791
- Python标准库笔记(5) — sched模块
事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...
- python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务
python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...
- Python标准库之sched模块介绍
sched——通用时间调度器 sched模块实现了一个通用事件调度器,在调度器类使用一个延迟函数等待特定的时间,执行任务.同时支持多线程应用程序,在每个任务执行后会立刻调用延时函数,以确保其他线程也能 ...
- python定时任务-sched模块
通过sched模块可以实现通过自定义时间,自定义函数,自定义优先级来执行函数. schedule = sched.scheduler( time.time,time.sleep) schedule是一 ...
- Python中使用os模块执行远程命令
1. 使用os模块远程执行命令 服务端代码 1 import socket 2 import os 3 4 sh_server = socket.socket() #创建一个socket对象 5 sh ...
随机推荐
- linux数据库中使用MD5加密
MD5加密算法源码下载:https://pan.baidu.com/s/1nwyN0xV 下载完成了之后解压,得到两个文件 环境搭建: 1.把md5.h文件拷贝到/usr/include/目录下 su ...
- 浅析RPO漏洞攻击原理
RPO的全称为Relative Path Overwrite,也就是相对路径覆盖,利用客户端和服务端的差异,通过相对路径来引入我们想要的js或者css文件,从而实现某种攻击. 就目前来看此攻击方法依赖 ...
- Maven使用第三方Jar文件
本例中,需要在Maven项目里添加uiautomator.jar文件.以下介绍两种方法: 方法一:在pom.xml里指定jar文件目录 <dependency> <groupId&g ...
- MongoDB出现The default storage engine 'wiredTiger' is not available之问题解决
问题描述:低版本MongoDB存在该问题(版本为3.x),高版本则无该问题. 参考解决问题链接:MongoDB学习—(1)安装时出现The default storage engine ‘wiredT ...
- CentOS7 安装nginx-1.14.0
nginx源码包:http://nginx.org/en/download.html 1.安装gcc gcc是用来编译下载下来的nginx源码 yum install gcc-c++ 2.安装pcre ...
- W tensorflow/core/util/ctc/ctc_loss_calculator.cc:144] No valid path found 或 loss:inf的解决方案
基于Tensorflow和Keras实现端到端的不定长中文字符检测和识别(文本检测:CTPN,文本识别:DenseNet + CTC),在使用自己的数据训练这个模型的过程中,出现如下错误,由于问题已经 ...
- JS提交表单页面不跳转、JS下载、动态创建from
JS下载 function downloadFile(id) { var url = "<%=request.getContextPath()%>/cer/downlo ...
- Oracle基础语法 一
表空间操作 1.创建一个表空间 create tablespace emp(空间名) datafile 'emp.dbf' size 10M autoextend on ; →表空间名字 / 物 ...
- ASP.Net Core中设置JSON中DateTime类型的格式化(解决时间返回T格式)
最近项目有个新同事,每个API接口里返回的时间格式中都带T如:[2019-06-06T10:59:51.1860128+08:00],其实这个主要是ASP.Net Core自带时间格式列化时间格式设置 ...
- Markdown温故知新(3):六个实用扩展语法
目录 1.表格(Table) 2.待办事项或清单(To Do List) 3.自动目录 TOC 4.流程图 5.时序图 6.甘特图 7.总结 1.表格(Table) 没用过 Markdown 表格的人 ...