在python上设置特定的间隔时间

在python上设置特定的间隔时间,python,Python,我目前正在从事一个Python项目。代码将在特定时间刷新,获取新记录并更新数据库。我想要达到的是每15分钟或30分钟刷新一次。下面的代码很好,但每天仅在00.00am获取一次新记录 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

我目前正在从事一个Python项目。代码将在特定时间刷新,获取新记录并更新数据库。我想要达到的是每15分钟或30分钟刷新一次。下面的代码很好,但每天仅在00.00am获取一次新记录

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

如果t.tm_min==15或30或45或00:行不正确

你想写的是

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

不管您怎么想,您的版本并不是比较Python中多个值的工作方式。由于即使第一次比较为false,其余值也为truthy,因此比较结果始终为true。相反,您希望检查该数字列表是否包含您的变量

如果var==15或var==30或var==45或var==00,您还应该显示
方法。如果t.tm_min in(15,30,45,00):>这非常有效,谢谢兄弟
if t.tm_min in (15,30,45,00):