欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)定時執(zhí)行任務(wù)的三種方式簡單示例

 更新時間:2019年03月30日 11:06:52   作者:YaoShuangQisBlogs  
這篇文章主要介紹了Python實現(xiàn)定時執(zhí)行任務(wù)的三種方式,結(jié)合簡單實例形式分析了Python使用time,os,sched等模塊定時執(zhí)行任務(wù)的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)定時執(zhí)行任務(wù)的三種方式。分享給大家供大家參考,具體如下:

1.定時任務(wù)代碼

#!/user/bin/env python
# @Time   :2018/6/7 16:31
# @Author  :PGIDYSQ
#@File   :PerformTaskTimer.py
#定時執(zhí)行任務(wù)命令
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,0,perform_command,(cmd,inc))
  schedule.run()
print('show time after 2 seconds:')
timming_exe('echo %time%',2)

2.周期性執(zhí)行任務(wù)

#!/user/bin/env python
# @Time   :2018/6/7 16:31
# @Author  :PGIDYSQ
#@File   :PerformTaskTimer.py
import time,os,sched
schedule = sched.scheduler(time.time,time.sleep)
def perform_command(cmd,inc):
  #在inc秒后再次運行自己,即周期運行
  schedule.enter(inc, 0, perform_command, (cmd, inc))
  os.system(cmd)
def timming_exe(cmd,inc=60):
  schedule.enter(inc,0,perform_command,(cmd,inc))
  schedule.run()#持續(xù)運行,直到計劃時間隊列變成空為止
print('show time after 2 seconds:')
timming_exe('echo %time%',2)

3.循環(huán)執(zhí)行命令

#!/user/bin/env python
# @Time   :2018/6/7 16:31
# @Author  :PGIDYSQ
#@File   :PerformTaskTimer.py
import time,os
def re_exe(cmd,inc = 60):
  while True:
    os.system(cmd)
    time.sleep(inc)
re_exe("echo %time%",5)

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論