python实现定时任务的方式_Python实现定时执行任务的三种方式简单示例
本文實例講述了Python實現定時執行任務的三種方式。分享給大家供大家參考,具體如下:
1.定時任務代碼
#!/user/bin/env python
# @Time :2018/6/7 16:31
# @Author :PGIDYSQ
[email?protected] :PerformTaskTimer.py
#定時執行任務命令
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.周期性執行任務
#!/user/bin/env python
# @Time :2018/6/7 16:31
# @Author :PGIDYSQ
[email?protected] :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()#持續運行,直到計劃時間隊列變成空為止
print('show time after 2 seconds:')
timming_exe('echo %time%',2)
3.循環執行命令
#!/user/bin/env python
# @Time :2018/6/7 16:31
# @Author :PGIDYSQ
[email?protected] :PerformTaskTimer.py
import time,os
def re_exe(cmd,inc = 60):
while True:
os.system(cmd)
time.sleep(inc)
re_exe("echo %time%",5)
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python实现定时任务的方式_Python实现定时执行任务的三种方式简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python socket传输图片_py
- 下一篇: python求非线性优化问题_用pyth