可以使用Python的datetime模塊來完成這個任務(wù)。首先,我們需要記錄上次運行時間,并且在每次程序運行結(jié)束后更新這個時間。然后,在下次運行程序時,就可以判斷當前時間與上次運行時間之間的時間間隔是否超過10分鐘。
下面是一個示例代碼:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/11290.html
import datetime import pickle import time def is_time_diff_greater_than_10_minutes(runtime): # TODO: 判斷上次運行時間和當前時間做對比 # 獲取當前時間 current_time = datetime.datetime.now() try: # 嘗試加載上次運行時間 with open('C:\windows\\temp\last_run_time.pkl', 'rb') as file: last_run_time = pickle.load(file) except FileNotFoundError: # 如果找不到上次運行時間文件,則認為是第一次運行程序 # 更新上次運行時間為當前時間,并保存到文件 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: # 計算時間間隔 time_diff = current_time - last_run_time # 判斷時間間隔是否大于10分鐘 if time_diff.total_seconds() / 60 > runtime: # 更新上次運行時間為當前時間,并保存到文件 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)維護-http://www.strong-digital.cn/11290.html
在上述代碼中,我們使用pickle模塊來對上次運行時間進行序列化和反序列化操作,以便將其保存到文件中。`is_time_diff_greater_than_10_minutes`函數(shù)會根據(jù)當前時間和上次運行時間的差值判斷是否大于10分鐘,并返回相應的結(jié)果。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/11290.html
請注意,此示例代碼僅供參考,實際使用中可能需要根據(jù)具體需求進行適當?shù)男薷摹?span id="hpp3dpzx7nj" class="beupset34">文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/11290.html
版權(quán)聲明:文章圖片資源來源于網(wǎng)絡(luò),如有侵權(quán),請留言刪除!!!
評論