python实现定时自动启动代码 线程方法(每日更新、爬虫等)
2020-06-27 05:29
1046 查看
本文是csdn的另一篇:python 定时器,每天凌晨3点执行 方法的更新
python实现定时自动启动代码 线程方法(每日更新、爬虫等)
本文使用threading.Timer(seconds, fun)的方法实现周期启动
通过datetime.datetime.now()等方法获取当前时间同时做校正判断
定时自启动函数的效果
如果想即食 直接修改marktime
然后在func加入你的代码或者调用你的函数
注释版
[code] import datetime import threading marktime=" 18:02:20" # 修改:上面的marktime改成你需要的自启动时间即可 # (空格别删了)请按照以上格式修改时间哦 乱修改会报错 # 运行函数 def func(): # 在这里加你的函数即可 print("haha") #如果需要循环调用,就要添加以下方法 timer = threading.Timer(86400, func) # 86400是3600*24 懂我意思吧 timer.start() # preFun预处理函数 对第一次启动进行今日或次日时间判断 然后方便进行正确的自启动 def preFun(): # 获取现在时间 now_time = datetime.datetime.now() marktimes = datetime.datetime.strptime(str(now_time.date()) + marktime, "%Y-%m-%d %H:%M:%S") # marktimes是datetime化的时间数据类型 # 2020-03-13 17:35:26.772379 marktimes是如左类似结构 # 今日时间是否预期 if (now_time <= marktimes): next_time = marktimes print("今日" + marktime + '执行代码') else: # 明日启动 next_time = now_time + datetime.timedelta(days=+1) print("明日" + marktime + '执行代码') # 这下面可以优化 懒得弄了……哈啊哈哈 next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + marktime, "%Y-%m-%d %H:%M:%S") # next_time将得到的下次时间更新成秒数表示的时间 # 科普一下 就是那种time.time()函数得到的秒 这样的时间方便计算 不然要疯狂的进制转化…… # 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。 # 获取距离下次marktime时间,单位为秒 timer_start_time = (next_time - now_time).total_seconds() return timer_start_time def main(): timer_start_time=preFun() # 把与处理得时间放进去 然后线程在start()后 # 会在规定秒数后启动你的func里的代码 timer = threading.Timer(timer_start_time, func) # 运行线程 timer.start() print('冷启动后启动func的时间',timer_start_time) pass if __name__ == '__main__': main()
简洁版
[code] import datetime import threading marktime=" 18:02:20" # 运行函数 def func(): # 在这里加你的函数即可 print("haha") timer = threading.Timer(86400, func) timer.start() # preFun预处理函数 def preFun(): now_time = datetime.datetime.now() marktimes = datetime.datetime.strptime(str(now_time.date()) + marktime, "%Y-%m-%d %H:%M:%S") if (now_time <= marktimes): next_time = marktimes print("今日" + marktime + '执行代码') else: # 明日启动 next_time = now_time + datetime.timedelta(days=+1) print("明日" + marktime + '执行代码') next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + marktime, "%Y-%m-%d %H:%M:%S") timer_start_time = (next_time - now_time).total_seconds() return timer_start_time def main(): timer_start_time=preFun() timer = threading.Timer(timer_start_time, func) timer.start() print('冷启动后启动func的时间',timer_start_time) pass if __name__ == '__main__': main()
总结
代码可以用但是性能没有测试……还有线程的中断是关闭进程,过于原始的方法显得非常不优雅。如果你感兴趣可以自己修改一下。
相关文章推荐
- python 每天如何定时启动爬虫任务(实现方法分享)
- 豆瓣机器人 自动加入/退出小组、自动在小组发帖/删帖、自动回复 Python实现代码 豆瓣爬虫
- 利用PHP安装windows自动运行的服务,PHP程序可以实现长时间、自动运行、定时更新功能,直接可以用在项目中的类源代码...
- 【代码】关于SQL的一些实现方法(不定时更新)
- 实现tomcat启动时自动运行代码的方法
- 用DEDECMS定时发布插件实现每日自动更新
- WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法
- 网页爬虫之cookie自动获取及过期自动更新的实现方法
- python实现dnspod自动更新dns解析的方法
- python爬虫教程:如何用40行Python代码实现天气预报和每日鸡汤推送功能
- Python实现定时自动关闭的tkinter窗口方法
- DedeCMS首页定时自动更新的实现方法
- Python + selenium + crontab实现每日定时自动打卡功能
- 利用PHP安装windows自动运行的服务,PHP程序可以实现长时间、自动运行、定时更新功能,直接可以用在项目中的类源代码
- python实现dnspod自动更新dns解析的方法
- 利用Python将每日一句定时推送至微信的实现方法
- Android在后台线程实现 定时更新时间
- Android应用自动更新功能的代码实现
- python 实现 自动定时发送人人状态
- [搜片神器]之DHT网络爬虫的代码实现方法