python 定时器,实现每天凌晨3点执行的方法

作者:姚贤贤 时间:2023-06-25 12:32:01 

如下所示:


'''
Created on 2018-4-20

例子:每天凌晨3点执行func方法
'''
import datetime
import threading

def func():
 print("haha")
 #如果需要循环调用,就要添加以下方法
 timer = threading.Timer(86400, func)
 timer.start()

# 获取现在时间
now_time = datetime.datetime.now()
# 获取明天时间
next_time = now_time + datetime.timedelta(days=+1)
next_year = next_time.date().year
next_month = next_time.date().month
next_day = next_time.date().day
# 获取明天3点时间
next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S")
# # 获取昨天时间
# last_time = now_time + datetime.timedelta(days=-1)

# 获取距离明天3点时间,单位为秒
timer_start_time = (next_time - now_time).total_seconds()
print(timer_start_time)
# 54186.75975

#定时器,参数为(多少时间后执行,单位为秒,执行的方法)
timer = threading.Timer(timer_start_time, func)
timer.start()

来源:https://blog.csdn.net/u011311291/article/details/80016859

标签:python,定时器
0
投稿

猜你喜欢

  • 浅谈vue项目利用Hbuilder打包成APP流程,以及遇到的坑

    2024-04-10 13:46:11
  • python爬虫之爬取百度音乐的实现方法

    2022-09-02 06:22:10
  • Laravel框架实现定时发布任务的方法

    2023-11-22 23:54:57
  • 如何用ASP输出HTML文件?

    2010-06-11 20:01:00
  • 微信跳一跳python辅助脚本(总结)

    2023-01-04 00:56:23
  • oracle 重置序列从指定数字开始的方法详解

    2023-07-05 02:40:04
  • Python Django模板系统详解

    2021-09-05 23:17:20
  • javascript仿qq界面的折叠菜单实现代码

    2024-06-12 17:12:49
  • Django自定义YamlField实现过程解析

    2021-01-14 16:43:20
  • Python IDE Pycharm中的快捷键列表用法

    2022-12-12 19:21:12
  • 用户体验中的五大要素

    2008-07-07 16:41:00
  • python反转(逆序)字符串的6种方法详细

    2023-03-14 10:38:41
  • Django ORM 查询管理器源码解析

    2023-01-30 12:28:47
  • 百度“有啊”首页首次曝光 以绿色调为主

    2008-10-20 12:52:00
  • ASP 高级模板引擎实现类

    2011-03-25 10:54:00
  • CentOS下安装Memcached和PHP Memcached扩展

    2023-11-14 14:50:19
  • 聊聊python 逻辑运算及奇怪的返回值(not,and,or)问题

    2021-08-07 01:04:42
  • 使用ASP调用C#写的COM组件

    2010-04-03 20:45:00
  • IE6,IE7中定位相关的怪异问题

    2009-12-08 12:49:00
  • PHP比你想象的好得多

    2023-11-20 09:33:30
  • asp之家 网络编程 m.aspxhome.com