可以使用Python的datetime模塊來(lái)完成這個(gè)任務(wù)。首先,我們需要記錄上次運(yùn)行時(shí)間,并且在每次程序運(yùn)行結(jié)束后更新這個(gè)時(shí)間。然后,在下次運(yùn)行程序時(shí),就可以判斷當(dāng)前時(shí)間與上次運(yùn)行時(shí)間之間的時(shí)間間隔是否超過(guò)10分鐘。
下面是一個(gè)示例代碼:文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11290.html
import datetime
import pickle
import time
def is_time_diff_greater_than_10_minutes(runtime): # TODO: 判斷上次運(yùn)行時(shí)間和當(dāng)前時(shí)間做對(duì)比
# 獲取當(dāng)前時(shí)間
current_time = datetime.datetime.now()
try:
# 嘗試加載上次運(yùn)行時(shí)間
with open('C:\windows\\temp\last_run_time.pkl', 'rb') as file:
last_run_time = pickle.load(file)
except FileNotFoundError:
# 如果找不到上次運(yùn)行時(shí)間文件,則認(rèn)為是第一次運(yùn)行程序
# 更新上次運(yùn)行時(shí)間為當(dāng)前時(shí)間,并保存到文件
with open('C:\windows\\temp\last_run_time.pkl', 'wb') as file:
pickle.dump(current_time, file)
last_run_time = None
if last_run_time:
# 計(jì)算時(shí)間間隔
time_diff = current_time - last_run_time
# 判斷時(shí)間間隔是否大于10分鐘
if time_diff.total_seconds() / 60 > runtime:
# 更新上次運(yùn)行時(shí)間為當(dāng)前時(shí)間,并保存到文件
with open('C:\windows\\temp\last_run_time.pkl', 'wb') as file:
pickle.dump(current_time, file)
return True
return False
while True:
if is_time_diff_greater_than_10_minutes(10):
print('大于10')
else:
print('小于10')
文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11290.html
在上述代碼中,我們使用pickle模塊來(lái)對(duì)上次運(yùn)行時(shí)間進(jìn)行序列化和反序列化操作,以便將其保存到文件中。`is_time_diff_greater_than_10_minutes`函數(shù)會(huì)根據(jù)當(dāng)前時(shí)間和上次運(yùn)行時(shí)間的差值判斷是否大于10分鐘,并返回相應(yīng)的結(jié)果。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11290.html
請(qǐng)注意,此示例代碼僅供參考,實(shí)際使用中可能需要根據(jù)具體需求進(jìn)行適當(dāng)?shù)男薷摹?span id="vfbtnhllzvbr" class="beupset58">文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11290.html


評(píng)論