如何使用python定时运行(Python实现定时执行任务的三种方式简单示例)
类别:脚本大全 浏览量:1456
时间:2021-10-24 10:42:38 如何使用python定时运行
Python实现定时执行任务的三种方式简单示例本文实例讲述了Python实现定时执行任务的三种方式。分享给大家供大家参考,具体如下:
1.定时任务代码
|
#!/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): 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 #@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() #持续运行,直到计划时间队列变成空为止 print ( 'show time after 2 seconds:' ) timming_exe( 'echo %time%' , 2 ) |
3.循环执行命令
|
#!/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 ) |
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/ysq0908/p/9157383.html
您可能感兴趣
- python创建字典的代码(Python创建字典的八种方式)
- python numpy矩阵详解(基于Numpy.convolve使用Python实现滑动平均滤波的思路详解)
- python中的冒号怎么看(python 列表中[ ]中冒号‘:’的作用)
- python编程ai人工智能(AI领域都在用Python即将被淘汰?网友预测未来的编程语言不会是TA)
- python自动解密(python实现移位加密和解密)
- python3常见问题(对Python3使运行暂停的方法详解)
- python函数的参数有几种类型(在Python中居然可以定义两个同名参数的函数)
- python中的数据类型和数字类型(Python数据类型之Number数字操作实例详解)
- python类中的数据封装(基于python生成器封装的协程类)
- python编辑redis(python使用pipeline批量读写redis的方法)
- python 怎么解析中文(Python中一般处理中文的几种方法)
- pythonftp功能介绍(使用Python操作FTP实现上传和下载的方法)
- python实现sql脚本规范(基于Python的SQL Server数据库实现对象同步轻量级)
- python装饰器使用说明(详解Python装饰器)
- python爬取数据总结(python3爬虫学习之数据存储txt的案例详解)
- Python实现模拟点击(用python实现刷点击率的示例代码)
- 日本菜有什么好吃(日本菜有什么好吃的做法)
- 韩国泡菜做法(韩国泡菜的做法步骤)
- 泰国旅游攻略(泰国旅游攻略必去景点)
- 越难春卷(越难春卷皮怎么用)
- 休闲VS新古典 办公家居简约设计(办公家居简约设计)
- 15个新成 园 位置公布 深圳龙岗2022年共建花园建设又有大动作(15个新成园位置公布)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9