Python3中常用的處理時(shí)間和實(shí)現(xiàn)定時(shí)任務(wù)的方法的介紹
無(wú)論哪種編程語(yǔ)言,時(shí)間肯定都是非常重要的部分,今天來(lái)看一下python如何來(lái)處理時(shí)間和python定時(shí)任務(wù),注意咯:本篇所講是python3版本的實(shí)現(xiàn),在python2版本中的實(shí)現(xiàn)略有不同,有時(shí)間會(huì)再寫(xiě)一篇以便大家區(qū)分。
1.計(jì)算明天和昨天的日期
#! /usr/bin/env python #coding=utf-8 # 獲取今天、昨天和明天的日期 # 引入datetime模塊 import datetime #計(jì)算今天的時(shí)間 today = datetime.date.today() #計(jì)算昨天的時(shí)間 yesterday = today - datetime.timedelta(days = 1) #計(jì)算明天的時(shí)間 tomorrow = today + datetime.timedelta(days = 1) #打印這三個(gè)時(shí)間 print(yesterday, today, tomorrow)
2.計(jì)算上一個(gè)的時(shí)間
方法一:
#! /usr/bin/env python #coding=utf-8 # 計(jì)算上一個(gè)的時(shí)間 #引入datetime,calendar兩個(gè)模塊 import datetime,calendar last_friday = datetime.date.today() oneday = datetime.timedelta(days = 1) while last_friday.weekday() != calendar.FRIDAY: last_friday -= oneday print(last_friday.strftime('%A, %d-%b-%Y'))
方法二:借助模運(yùn)算尋找上一個(gè)星期五
#! /usr/bin/env python #coding=utf-8 # 借助模運(yùn)算,可以一次算出需要減去的天數(shù),計(jì)算上一個(gè)星期五 #同樣引入datetime,calendar兩個(gè)模塊 import datetime import calendar today = datetime.date.today() target_day = calendar.FRIDAY this_day = today.weekday() delta_to_target = (this_day - target_day) % 7 last_friday = today - datetime.timedelta(days = delta_to_target) print(last_friday.strftime("%d-%b-%Y"))
3.計(jì)算歌曲的總播放時(shí)間
#! /usr/bin/env python #coding=utf-8 # 獲取一個(gè)列表中的所有歌曲的播放時(shí)間之和 import datetime def total_timer(times): td = datetime.timedelta(0) duration = sum([datetime.timedelta(minutes = m, seconds = s) for m, s in times], td) return duration times1 = [(2, 36), (3, 35), (3, 45), ] times2 = [(3, 0), (5, 13), (4, 12), (1, 10), ] assert total_timer(times1) == datetime.timedelta(0, 596) assert total_timer(times2) == datetime.timedelta(0, 815) print("Tests passed.\n" "First test total: %s\n" "Second test total: %s" % (total_timer(times1), total_timer(times2)))
4.反復(fù)執(zhí)行某個(gè)命令
#! /usr/bin/env python #coding=utf-8 # 以需要的時(shí)間間隔執(zhí)行某個(gè)命令 import time, os def re_exe(cmd, inc = 60): while True: os.system(cmd); time.sleep(inc) re_exe("echo %time%", 5)
5.定時(shí)任務(wù)
#! /usr/bin/env python #coding=utf-8 #這里需要引入三個(gè)模塊 import time, os, sched # 第一個(gè)參數(shù)確定任務(wù)的時(shí)間,返回從某個(gè)特定的時(shí)間到現(xiàn)在經(jīng)歷的秒數(shù) # 第二個(gè)參數(shù)以某種人為的方式衡量時(shí)間 schedule = sched.scheduler(time.time, time.sleep) def perform_command(cmd, inc): os.system(cmd) def timming_exe(cmd, inc = 60): # enter用來(lái)安排某事件的發(fā)生時(shí)間,從現(xiàn)在起第n秒開(kāi)始啟動(dòng) schedule.enter(inc, 0, perform_command, (cmd, inc)) # 持續(xù)運(yùn)行,直到計(jì)劃時(shí)間隊(duì)列變成空為止 schedule.run() print("show time after 10 seconds:") timming_exe("echo %time%", 10)
6.利用sched實(shí)現(xiàn)周期調(diào)用
#! /usr/bin/env python #coding=utf-8 import time, os, sched # 第一個(gè)參數(shù)確定任務(wù)的時(shí)間,返回從某個(gè)特定的時(shí)間到現(xiàn)在經(jīng)歷的秒數(shù) # 第二個(gè)參數(shù)以某種人為的方式衡量時(shí)間 schedule = sched.scheduler(time.time, time.sleep) def perform_command(cmd, inc): # 安排inc秒后再次運(yùn)行自己,即周期運(yùn)行 schedule.enter(inc, 0, perform_command, (cmd, inc)) os.system(cmd) def timming_exe(cmd, inc = 60): # enter用來(lái)安排某事件的發(fā)生時(shí)間,從現(xiàn)在起第n秒開(kāi)始啟動(dòng) schedule.enter(inc, 0, perform_command, (cmd, inc)) # 持續(xù)運(yùn)行,直到計(jì)劃時(shí)間隊(duì)列變成空為止 schedule.run() print("show time after 10 seconds:") timming_exe("echo %time%", 10)
- Python實(shí)現(xiàn)定時(shí)任務(wù)
- Linux下Python腳本自啟動(dòng)與定時(shí)任務(wù)詳解
- 詳解使用python crontab設(shè)置linux定時(shí)任務(wù)
- Python中定時(shí)任務(wù)框架APScheduler的快速入門(mén)指南
- python Celery定時(shí)任務(wù)的示例
- Python定時(shí)任務(wù)sched模塊用法示例
- Python3實(shí)現(xiàn)定時(shí)任務(wù)的四種方式
- 解決Python中定時(shí)任務(wù)線程無(wú)法自動(dòng)退出的問(wèn)題
- Linux部署python爬蟲(chóng)腳本,并設(shè)置定時(shí)任務(wù)的方法
- python BlockingScheduler定時(shí)任務(wù)及其他方式的實(shí)現(xiàn)
相關(guān)文章
深入理解Python虛擬機(jī)中浮點(diǎn)數(shù)(float)的實(shí)現(xiàn)原理及源碼
在本篇文章當(dāng)中主要分析在 cpython 虛擬機(jī)當(dāng)中 float 類(lèi)型的實(shí)現(xiàn)原理以及與他相關(guān)的一些源代碼,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-03-03python數(shù)字圖像處理skimage讀取顯示與保存圖片
這篇文章主要為大家介紹了python數(shù)字圖像處理使用skimage讀取顯示與保存圖片示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python實(shí)現(xiàn)xml格式轉(zhuǎn)txt格式的示例代碼
VOC 的標(biāo)注是xml格式的,而YOLO是.txt格式,所以要實(shí)現(xiàn)VOC數(shù)據(jù)集轉(zhuǎn)YOLO數(shù)據(jù)集,只能利用代碼實(shí)現(xiàn)。所以本文為大家介紹了Python中xml轉(zhuǎn)txt的示例代碼,需要的可以參考一下2022-03-03pytorch中.to(device) 和.cuda()的區(qū)別說(shuō)明
這篇文章主要介紹了pytorch中.to(device) 和.cuda()的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python使用Slider組件實(shí)現(xiàn)調(diào)整曲線參數(shù)功能示例
這篇文章主要介紹了Python使用Slider組件實(shí)現(xiàn)調(diào)整曲線參數(shù)功能,結(jié)合實(shí)例形式分析了Python使用matplotlib與Slider組件進(jìn)行圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09