python特定时间段运行程序

import requests
import time
from datetime import datetime, timedelta
'''
死循环程序在每天8:45~23:45期间,每隔1小时发送信息
代码逻辑:=======
死循环运行:
    判断当前时间 in 今天8:45~今天23:45   
        判断当前时间 before xx:45
            程序休眠至 xx:45, 再运行send_msg()
        else:当前时间 after xx:45
            程序休眠至 下一个xx:45, 再运行send_msg()
            
    else:
        判断当前时间 before 今天8:45
            程序休眠至 今天8:45, 再运行send_msg()
        else:当前时间 after 今天23:45
            程序休眠至 明天8:45, 再运行send_msg()
    
'''

def send_msg():
    print(requests.get('https://www.baidu.com'))
    time.sleep(60)

while True:
    today_now = datetime.now()
    print(today_now)
    today_at_0845 = today_now.replace(hour=8, minute=45, second=0)
    today_at_2345 = today_now.replace(hour=23,minute=45, second=0)
    if today_at_0845 <= today_now <= today_at_2345:
        diff45 = today_now.replace(minute=45, second=0) - today_now
        if 0 <= diff45.days:
            print('before xx:45, sleep....')
            time.sleep(diff45.seconds)
        else:
            print('after xx:45, sleeping...')
            diff45 = today_now.replace(hour=today_now.hour + 1, minute=45, second=0) - today_now
            time.sleep(diff45.seconds)
    else:
        print('no in', '='*30)
        diff = today_at_0845 - today_now
        if 0 <= diff.days:
            print('before today_at_0845, sleep...')
            time.sleep(diff.seconds)
        else:
            print('after today_at_2345, sleep...')
            tomorrow_at_0845 = today_now.replace(hour=8, minute=45, second=0) + timedelta(days=1)
            diff = tomorrow_at_0845 - today_now
            time.sleep(diff.seconds)
    send_msg()


def sleeping():
    '''
    对上面的代码进行封装。
    用于实现每天的定时拨测:从8:45到23:45,每隔一个小时
    :return:
    '''
    begin_hour = 8
    minute_point = 45
    end_hour = 23
    per_hour = 1

    today_now = datetime.now()
    print(today_now)
    today_at_0845 = today_now.replace(hour=begin_hour, minute=minute_point, second=0) # begin time
    today_at_2345 = today_now.replace(hour=end_hour, minute=minute_point, second=0) # end time
    if today_at_0845 <= today_now <= today_at_2345:
        diff45 = today_now.replace(minute=minute_point, second=0) - today_now
        if 0 <= diff45.days:
            print(f'before xx:{minute_point}, sleep....')
            time.sleep(diff45.seconds)
        else:
            print(f'after xx:{minute_point}, sleeping...')
            # diff45 = today_now.replace(hour=today_now.hour + 1, minute=minute_point, second=0) - today_now
            diff45 = today_now.replace(minute=minute_point, second=0) - today_now + timedelta(hours=per_hour)
            time.sleep(diff45.seconds)
    else:
        print('no in', '=' * 30)
        # my_logger.info('no in', '=' * 30)
        diff = today_at_0845 - today_now
        if 0 <= diff.days:
            print(f'before today_at_08{minute_point}, sleep...')
            time.sleep(diff.seconds)
        else:
            print(f'after today_at_23{minute_point}, sleep...')
            tomorrow_at_0845 = today_at_0845 + timedelta(days=1)
            diff = tomorrow_at_0845 - today_now
            time.sleep(diff.seconds)

其他模块补充:

import schedule
import time
import random
from datetime import datetime

def perform_health_check():
    # 模拟拨测过程,随机耗时 5 到 10 分钟
    today_now = datetime.now()
    print(today_now)
    duration = random.randint(5, 10)
    print("Performing health check... (Duration: {} minutes)".format(duration))
    time.sleep(duration * 60)
    print("Health check completed.")


def schedule_health_check():
    # 定义每小时的 45 分执行拨测任务
    # schedule.every().hour.at(":45").do(perform_health_check)
    schedule.every().day.at("08:45").do(perform_health_check)
    schedule.every().day.at("09:45").do(perform_health_check)
    schedule.every().day.at("22:45").do(perform_health_check)
    schedule.every().day.at("23:45").do(perform_health_check)
    # 持续运行调度程序
    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == "__main__":
    # perform_health_check()
    schedule_health_check()

如果可以,也可以通过LIUNX编写定时任务

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值