使用Python怎么设置随机时间执行定时任务

  介绍

使用Python怎么设置随机时间执行定时任务?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

Python主要用来做什么

Python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。

import 日志记录   import 回溯   得到datetime  import  datetime   得到apscheduler.schedulers.background  import  BackgroundScheduler   时间=scheduler  BackgroundScheduler ()   def  spider_schedule ():   #,才能spider_schedule是job_id   scheduler.remove_job才能(& # 39;spider_schedule& # 39;)   尝试才能:   ,,,print  & # 39; spider 开始…,& # 39;,datetime.now () .strftime (& # 39; Y % - % - % d  % x # 39;)   ,,,# - - - - - - - - - -自己的业务代码- - - - - - -   ,才能通过   ,,,# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   ,,,print  & # 39; spider 结束…,& # 39;,datetime.now () .strftime (& # 39; Y % - % - % d  % x # 39;)   except 才能;Exception  as  e:   ,,,print  traceback.format_exc (e)   最后才能:   ,,,interval_minutes =, random.randint(60, 120), #, 1 - 120分钟随机选一个时间   ,,,interval_seconds =, random.randint(1, 60), #, 1 ~ 60秒随机选一个时间   ,,,scheduler.add_job (spider_schedule, & # 39;间隔# 39;,,分钟=interval_minutes,秒=interval_seconds, id=& # 39; spider_schedule& # 39;)   if  __name__ ==, & # 39; __main__ # 39;:   scheduler.add_job才能(spider_schedule, & # 39;间隔# 39;,,秒=10,id=& # 39; spider_schedule& # 39;)   scheduler.start才能()

<>强ps:下面看下python定时执行任务的三种方式

#定时执行任务命令   # 1。定时任务代码   import 时间,操作系统,固定播送时间=#,schedule  sched.scheduler (time.time time . sleep)   #,def  perform_command (cmd公司):   #,,,os.system (cmd)   #,,,印刷(& # 39;任务# 39;)   #,def  timming_exe (cmd公司=60):   #,,,schedule.enter (inc, 0, perform_command, (cmd公司))   #,,,schedule.run ()   #,打印(& # 39;show  time  after  2,秒:& # 39;)   #,timming_exe (& # 39; echo  % % & # 39; (2)   # 2。周期性执行任务   时间=schedule  sched.scheduler (time.time time . sleep)   def  perform_command (cmd公司):   #在公司才能秒后再次运行自己,即周期运行   schedule.enter才能(公司,0,,perform_command,, (cmd, inc .))   os.system才能(cmd)   def  timming_exe (cmd公司=60):   schedule.enter才能(公司0 perform_command, (cmd公司))   schedule.run才能()#持续运行,直到计划时间队列变成空为止   打印(& # 39;show  time  after  2,秒:& # 39;)   timming_exe (& # 39; echo  % % & # 39; (2)   # 3。循环执行命令   #,import 时间,操作系统   #,def  re_exe (cmd, inc =, 60):   #,,,while 正确的:   #,,,,,os.system (cmd)   #,,,,,time . sleep (inc)   #,re_exe (“echo  % %时间“,5)

关于使用Python怎么设置随机时间执行定时任务问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

使用Python怎么设置随机时间执行定时任务