python定时执行详解
知识点 1. sched模块,准确的说,它是一个调度(延时处理机制),每次想要定时执行某任务都必须写入一个调度。 (1)生成调度器: 2. time模块,它是python自带的模块,主要用于时间的格式转换和处理。 time.sleep(s) 推迟调用线程的运行,s指秒数 3. os模块也是python自带的模块,os模块中的system()函数可以方便地运行其他程序或者脚本。 os.system(cmd) 下面我们来看具体实例: 1.定时任务代码 #定时执行任务命令 import time,os,sched schedule = sched.scheduler(time.time,time.sleep) def perform_command(cmd,inc): os.system(cmd) print('task') def timming_exe(cmd,inc=60): schedule.enter(inc,perform_command,(cmd,inc)) schedule.run() show time after 2 seconds:) timming_exe(echo %time%',2) 2.周期性执行任务 在inc秒后再次运行自己,即周期运行 schedule.enter(inc,inc)) os.system(cmd) 持续运行,直到计划时间队列变成空为止def re_exe(cmd,inc = 60): while True: os.system(cmd) time.sleep(inc) re_exe("",5) ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |