python如何每天在指定时间段运行程序及关闭程序

作者:Lee-Oct 时间:2021-08-05 05:42:05 

python每天在指定时间段运行程序及关闭程序

场景

程序需要在每天某一时间段内运行,然后在某一时间段内停止该程序。

程序:

from datetime import datetime, time
import multiprocessing
from time import sleep
# 程序运行时间在白天8:30 到 15:30  晚上20:30 到 凌晨 2:30
DAY_START = time(8, 30)
DAY_END = time(15, 30)
NIGHT_START = time(20, 30)
NIGHT_END = time(2, 30)
def run_child():
   while 1:
       print("正在运行子进程")
def run_parent():
   print("启动父进程")
   child_process = None  # 是否存在子进程
   while True:
       current_time = datetime.now().time()
       running = False  # 子进程是否可运行
       if DAY_START <= current_time <= DAY_END or (current_time >= NIGHT_START) or (current_time <= NIGHT_END):
           # 判断时候在可运行时间内
           running = True
       # 在时间段内则开启子进程
       if running and child_process is None:
           print("启动子进程")
           child_process = multiprocessing.Process(target=run_child)
           child_process.start()
           print("子进程启动成功")
       # 非记录时间则退出子进程
       if not running and child_process is not None:
           print("关闭子进程")
           child_process.terminate()
           child_process.join()
           child_process = None
           print("子进程关闭成功")
       sleep(5)
if __name__ == '__main__':
   run_parent()

python定时程序(每隔一段时间执行指定函数)

import os
import time
def print_ts(message):
   print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message)
def run(interval, command):
   print_ts("-"*100)
   print_ts("Command %s"%command)
   print_ts("Starting every %s seconds."%interval)
   print_ts("-"*100)
   while True:
       try:
           # sleep for the remaining seconds of interval
           time_remaining = interval-time.time()%interval
           print_ts("Sleeping until %s (%s seconds)..."%((time.ctime(time.time()+time_remaining)), time_remaining))
           time.sleep(time_remaining)
           print_ts("Starting command.")
           # execute the command
           status = os.system(command)
           print_ts("-"*100)
           print_ts("Command status = %s."%status)
       except Exception, e:
           print e
if __name__=="__main__":
   interval = 5
   command = r"ls"
   run(interval, command)

来源:https://blog.csdn.net/weixin_35737303/article/details/99561792

标签:python,时间,运行程序,关闭程序
0
投稿

猜你喜欢

  • PyGame实现初始化导入所有模块方法详解

    2023-05-25 15:14:55
  • TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)

    2022-11-08 00:54:03
  • python pip特殊用法之pip install -v -e .命令详解

    2022-07-02 09:56:43
  • ASP+SQL Server构建网页防火墙

    2009-01-21 19:56:00
  • Python游戏开发之Pygame使用的最全教程分享

    2021-01-11 05:45:55
  • Python切片用法实例教程

    2023-09-28 15:26:17
  • 跟老齐学Python之print详解

    2021-02-06 03:32:56
  • 对python中assert、isinstance的用法详解

    2022-04-29 14:10:54
  • python xlsxwriter库生成图表的应用示例

    2022-03-02 20:01:33
  • 匹配 IP 地址与域名的正则表达式

    2023-06-17 05:55:48
  • 新手教程:如何设置五大类MySQL参数

    2010-03-03 16:40:00
  • 微信小程序实现图片上传、删除和预览功能的方法

    2023-09-20 08:54:30
  • Python爬虫框架-scrapy的使用

    2022-09-11 20:12:28
  • pytorch教程之网络的构建流程笔记

    2021-11-24 10:58:59
  • Python如何使用字符打印照片

    2023-06-12 09:20:34
  • Python中字符串类型代码的执行函数——eval()、exec()和compile()详解

    2022-07-08 09:14:12
  • 完全卸载MYSQL

    2011-02-23 12:11:00
  • SQL根据时间范围条件查询数据

    2009-01-18 13:32:00
  • python批量修改xml文件中的信息

    2022-03-29 22:50:53
  • python fire库的使用实例教程

    2023-08-24 14:01:22
  • asp之家 网络编程 m.aspxhome.com