每五分鐘運行一次命令的實現

繞進去沒繞出來,在死胡同里越繞越深。
出去上了個廁所,豁然開朗。
不就是想每5分鐘運行一個命令,只允許一次嗎?

繞進死胡同的思路

正確的思路:

import timer
import time
import os
while True:

if timer.func(5) is True:
    print "Do Something"
    time.sleep(60)        #直接睡過去一分鐘不就好了嗎?
print time.ctime()[11:-5]

所以說,發現問題解決不了的時候要及時跳開,想想到底想要什么,而不是拘泥于細節死扣。
越死扣,越著急,越解決不了。

程序的作用是在00,05,10,15,20,25,30,....這種特定的分鐘跳出循環執行命令。可以用來使重復采樣實現。

# Countdown Timer
# Designed By Xc.Li @ Mar.2016
# coding = utf-8
import time
import os
def func(mode):
if mode == 5:
    check_point = [00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
if mode == 10:
    check_point = [00, 10, 20, 30, 40, 50]
if mode == 15:
    check_point = [00, 15, 30, 45]
if mode == "test":
    check_point = [00, 10, 20, 23]
while True:
    current_minute = time.ctime()[14:-8]
    for item in check_point:
        if str(item) == current_minute or current_minute == '00':
            print "Check Point!"
            time.sleep(5)
            return True
    print "Waiting at mode %d" % mode
    time.sleep(5)
    os.system("cls")
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容