#!/user/bin/env python#@Time: 2018/6/7 16:31#@Author:P gidysq#@File:P erformtasktimer.py#scheduled execution of task commands#1. Timed Task CodeImporttime,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,0,perform_command, (cmd,inc))#Schedule.run ()#print (' Show time after 2 seconds: ')#Timming_exe (' Echo%time% ', 2)#2. Periodic execution of tasksSchedule =Sched.scheduler (time.time,time.sleep)defPerform_command (cmd,inc):#Run yourself again in inc seconds, i.e. cycle runSchedule.enter (inc, 0, Perform_command, (CMD, Inc)) Os.system (cmd)defTimming_exe (cmd,inc=60): Schedule.enter (Inc,0,perform_command, (cmd,inc)) Schedule.run ()#run until the scheduled time queue becomes emptyPrint('Show time after 2 seconds:') Timming_exe ('Echo%time%', 2)#3. Cyclic execution of commands#Import Time,os#def re_exe (cmd,inc =):#While True:#os.system (cmd)#Time.sleep (inc)#Re_exe ("Echo%time%", 5)
Three ways Python performs tasks on a timed basis