Three ways Python performs tasks on a timed basis

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.