简体   繁体   English

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

[英]setting specific interval time on python

I'm currently working on a Python project. 我目前正在研究Python项目。 The code will refresh at a specific time and fetch new records and update the database. 该代码将在特定时间刷新并获取新记录并更新数据库。 What I'm trying to achieve is to refresh every 15 minutes or 30 minutes. 我想要实现的是每15分钟或30分钟刷新一次。 The below code is good but only fetches new records once per day at 00.00am. 以下代码不错,但每天凌晨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()

I tried the below but it's refreshing every second 我尝试了以下内容,但每秒刷新一次

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

The line if t.tm_min == 15 or 30 or 45 or 00: is incorrect. if t.tm_min == 15 or 30 or 45 or 00:的行不正确。

What you want to write instead is 您要写的是

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

Despite what you may think, your version is not how comparing to multiple values works in Python. 尽管您可能会想,但您的版本并不是在Python中与多个值进行比较的方式。 The comparison will always evaluate to true since even if the first comparison is false, the rest of the values are truthy. 该比较将始终为true,因为即使第一个比较为false,其余值也为true。 You instead want to check if that list of number contains your variable. 相反,您想检查该数字列表是否包含您的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM