通过Python3实现任务的定时循环执行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了通过Python3实现任务的定时循环执行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3615字,纯文字阅读大概需要6分钟。
内容图文
![通过Python3实现任务的定时循环执行](/upload/InfoBanner/zyjiaocheng/427/95a7ea36eaa34e7896afd09368327f0d.jpg)
比如使用Python在进行爬虫系统开发时可能就需要间隔一段时间就重复执行的任务的需求,从而实现一个线程服务在后台监控数据的抓取状态,这里定时器就可以帮忙了。
【视频推荐:Python3视频教程】
【手册推荐:Python中文手册】
通过Python的文档我们可以找到threading.Timer()来实现定时功能:
简单实现代码:
import threading def func1(a): #Do something print('Do something') a+=1 print(a) print('当前线程数为{}'.format(threading.activeCount())) if a>5: return t=threading.Timer(5,func1,(a,)) t.start()
效果图:
通过查阅资料,利用Python能实现三种不同的定时任务执行方式:
1.定时任务代码
#!/user/bin/env python #定时执行任务命令 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 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 import time,os def re_exe(cmd,inc = 60): while True: os.system(cmd) time.sleep(inc) re_exe("echo %time%",5)
总结而言:Python实现定时器的方法都是schedule和threading的实现,具体的用法还要根据实际情况灵活运用。
最常用的两个模块:threading、Sched
threading模块使用:
import threading ,time from time import sleep, ctime class Timer(threading.Thread): """ very simple but useless timer. """ def __init__(self, seconds): self.runTime = seconds threading.Thread.__init__(self) def run(self): time.sleep(self.runTime) print ("Buzzzz!! Time's up!") class CountDownTimer(Timer): """ a timer that can counts down the seconds. """ def run(self): counter = self.runTime for sec in range(self.runTime): print (counter) time.sleep(1.0) counter -= 1 print ("Done") class CountDownExec(CountDownTimer): """ a timer that execute an action at the end of the timer run. """ def __init__(self, seconds, action, args=[]): self.args = args self.action = action CountDownTimer.__init__(self, seconds) def run(self): CountDownTimer.run(self) self.action(self.args) def myAction(args=[]): print ("Performing my action with args:") print (args) if __name__ == "__main__": t = CountDownExec(3, myAction, ["hello", "world"]) t.start() print("2333")
Sched模块使用:
''' 使用sched模块实现的timer,sched模块不是循环的,一次调度被执行后就Over了,如果想再执行, 可以使用while循环的方式不停的调用该方法 ''' import time, sched #被调度触发的函数 def event_func(msg): print("Current Time:", time.strftime("%y-%m-%d %H:%M:%S"), 'msg:', msg) def run_function(): #初始化sched模块的scheduler类 s = sched.scheduler(time.time, time.sleep) #设置一个调度,因为time.sleep()的时间是一秒,所以timer的间隔时间就是sleep的时间,加上enter的第一个参数 s.enter(0, 2, event_func, ("Timer event.",)) s.run() def timer1(): while True: #sched模块不是循环的,一次调度被执行后就Over了,如果想再执行,可以使用while循环的方式不停的调用该方法 time.sleep(1) run_function() if __name__ == "__main__": timer1()
以上就是通过Python3实现任务的定时循环执行的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的通过Python3实现任务的定时循环执行全部内容,希望文章能够帮你解决通过Python3实现任务的定时循环执行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。