簡體   English   中英

在python上設置特定的間隔時間

[英]setting specific interval time on python

我目前正在研究Python項目。 該代碼將在特定時間刷新並獲取新記錄並更新數據庫。 我想要實現的是每15分鍾或30分鍾刷新一次。 以下代碼不錯,但每天凌晨00.00只獲取一次新記錄。

def check_schedule_task(self):
        # update group member list at 00:00 am every morning
        t = time.localtime()
        if t.tm_hour == 0 and t.tm_min <= 1:
            # update group member
            Log.debug('update group member list everyday')
            self.db.delete_table(Constant.TABLE_GROUP_LIST())
            self.db.delete_table(Constant.TABLE_GROUP_USER_LIST())
            self.db.create_table(Constant.TABLE_GROUP_LIST(), Constant.TABLE_GROUP_LIST_COL)
            self.db.create_table(Constant.TABLE_GROUP_USER_LIST(), Constant.TABLE_GROUP_USER_LIST_COL)
self.wechat.fetch_group_contacts()

我嘗試了以下內容,但每秒刷新一次

def check_schedule_task(self):
            # update group member list at 00:00 am every morning
            t = time.localtime()
            if t.tm_min == 15 or 30 or 45 or 00:
                # update group member
                Log.debug('update group member list everyday')
                self.db.delete_table(Constant.TABLE_GROUP_LIST())
                self.db.delete_table(Constant.TABLE_GROUP_USER_LIST())
                self.db.create_table(Constant.TABLE_GROUP_LIST(), Constant.TABLE_GROUP_LIST_COL)
                self.db.create_table(Constant.TABLE_GROUP_USER_LIST(), Constant.TABLE_GROUP_USER_LIST_COL)
    self.wechat.fetch_group_contacts()

if t.tm_min == 15 or 30 or 45 or 00:的行不正確。

您要寫的是

if t.tm_min in (15,30,45,00):

盡管您可能會想,但您的版本並不是在Python中與多個值進行比較的方式。 該比較將始終為true,因為即使第一個比較為false,其余值也為true。 相反,您想檢查該數字列表是否包含您的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM