【2014-03-05】【转】Python3 - 时间处理与定时任务

📅 2026/8/20 14:10:46
【2014-03-05】【转】Python3 - 时间处理与定时任务
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-03-05 标题【转】Python3 - 时间处理与定时任务分类编程 / python jython 标签python jython·定时【转】Python3 - 时间处理与定时任务Python3 - 时间处理与定时任务4.反复执行某个命令5.定时任务6.利用sched实现周期调用本文转自【 http://www.pythontab.com/html/2013/pythonjichu_0119/146.html 】有删改。Python3 - 时间处理与定时任务无论哪种编程语言时间肯定都是非常重要的部分今天来看一下python如何来处理时间和python定时任务注意咯本篇所讲是python3版本的实现在python2版本中的实现略有不同有时间会再写一篇以便大家区分。。。。。。。。。。。。。。。4.反复执行某个命令#! /usr/bin/env python#codingutf-8# 以需要的时间间隔执行某个命令importtime,osdefre_exe(cmd,inc60):whileTrue:os.system(cmd);time.sleep(inc)re_exe(echo %time%,5)5.定时任务#! /usr/bin/env python#codingutf-8#这里需要引入三个模块importtime,os,sched# 第一个参数确定任务的时间返回从某个特定的时间到现在经历的秒数# 第二个参数以某种人为的方式衡量时间schedulesched.scheduler(time.time,time.sleep)defperform_command(cmd,inc):os.system(cmd)deftimming_exe(cmd,inc60):# enter用来安排某事件的发生时间从现在起第n秒开始启动schedule.enter(inc,0,perform_command,(cmd,inc))# 持续运行直到计划时间队列变成空为止schedule.run()print(show time after 10 seconds:)timming_exe(echo %time%,10)6.利用sched实现周期调用#! /usr/bin/env python#codingutf-8importtime,os,sched# 第一个参数确定任务的时间返回从某个特定的时间到现在经历的秒数# 第二个参数以某种人为的方式衡量时间schedulesched.scheduler(time.time,time.sleep)defperform_command(cmd,inc):# 安排inc秒后再次运行自己即周期运行schedule.enter(inc,0,perform_command,(cmd,inc))os.system(cmd)deftimming_exe(cmd,inc60):# enter用来安排某事件的发生时间从现在起第n秒开始启动schedule.enter(inc,0,perform_command,(cmd,inc))# 持续运行直到计划时间队列变成空为止schedule.run()print(show time after 10 seconds:)timming_exe(echo %time%,10)