2017-07-20 98 views
0

我目前正在開發一個Python項目。代碼將在特定時間刷新並獲取新記錄並更新數據庫。我想要實現的是每15分鐘或30分鐘刷新一次。下面的代碼是很好的,但只在每天上午00點提取一次新記錄。在python上設置特定的時間間隔

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() 

回答

0

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

你想,而不是寫什麼是

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

儘管你可能會認爲,你的版本是不如何在Python比較多個值的作品。比較將始終評估爲真,因爲即使第一次比較爲假,其餘值也是真實的。你反而想檢查這個數字列表是否包含你的變量。

+0

您還應該顯示'if var == 15或var == 30或var == 45或var == 00'方法。 – 3D1T0R

+0

如果t.tm_min在(15,30,45,00):>這個很好用,謝謝兄弟 –