[英]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.