每 3 分钟检查一次条件,无功能且不中断循环
Check a condition every 3 minutes without functions and without interrupting the loop
我有这个工作代码,考虑到当地时间,每 3 分钟检查一次条件,所以每 0、3、6、9 ......它打印“检查条件”。
import time
def get_next_time():
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
return result
next_run = get_next_time()
while True:
now = time.localtime()
if next_run == now.tm_min:
print("checking condition")
#some condition
next_run = get_next_time()
time.sleep(1)
问题是我需要没有函数的代码,所以我需要找到一种不使用任何函数来编写这段代码的方法,而且我不能使用 break 或 interrput 循环
我试过了:
while True:
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
now = time.localtime()
if result == now.tm_min:
print("checking conditions")
time.sleep(1)
但它不起作用:它什么都不做。
有什么想法吗?
您可以在一条语句中压缩函数:
import time
next_run = (3 - (time.localtime().tm_min % 3) + time.localtime().tm_min)%60
while True:
now = time.localtime()
if next_run == now.tm_min:
print("checking condition")
#checking conditions...
next_run=(3 - (time.localtime().tm_min % 3) + time.localtime().tm_min)%60
time.sleep(1)
第一次,get_next_time()
只会在next_run == now.tm_min
时执行。第二次,你每次循环执行它
import time
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
while True:
now = time.localtime()
if result == now.tm_min:
print("checking conditions")
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
time.sleep(1)
四舍五入到下一个 3 分钟的倍数与“每 0...”的规范相矛盾。
够了
import time
first= True
while True:
minute= time.localtime().tm_min
if first or minute == target:
print("checking condition")
first= False
target= (minute + 3) % 60
time.sleep(1)
更新:
我修改了代码,以便在每次迭代时对 localtime
进行一次调用,以完全确保分钟数不会在两次调用之间发生变化。
更紧凑但效率更低:
import time
while True:
minute= time.localtime().tm_min
if 'target' not in locals() or minute == target:
print("checking condition")
target= (minute + 3) % 60
time.sleep(1)
我有这个工作代码,考虑到当地时间,每 3 分钟检查一次条件,所以每 0、3、6、9 ......它打印“检查条件”。
import time
def get_next_time():
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
return result
next_run = get_next_time()
while True:
now = time.localtime()
if next_run == now.tm_min:
print("checking condition")
#some condition
next_run = get_next_time()
time.sleep(1)
问题是我需要没有函数的代码,所以我需要找到一种不使用任何函数来编写这段代码的方法,而且我不能使用 break 或 interrput 循环
我试过了:
while True:
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
now = time.localtime()
if result == now.tm_min:
print("checking conditions")
time.sleep(1)
但它不起作用:它什么都不做。 有什么想法吗?
您可以在一条语句中压缩函数:
import time
next_run = (3 - (time.localtime().tm_min % 3) + time.localtime().tm_min)%60
while True:
now = time.localtime()
if next_run == now.tm_min:
print("checking condition")
#checking conditions...
next_run=(3 - (time.localtime().tm_min % 3) + time.localtime().tm_min)%60
time.sleep(1)
第一次,get_next_time()
只会在next_run == now.tm_min
时执行。第二次,你每次循环执行它
import time
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
while True:
now = time.localtime()
if result == now.tm_min:
print("checking conditions")
minute = time.localtime().tm_min
result = 3 - (minute % 3) + minute
if result == 60:
result = 0
time.sleep(1)
四舍五入到下一个 3 分钟的倍数与“每 0...”的规范相矛盾。
够了
import time
first= True
while True:
minute= time.localtime().tm_min
if first or minute == target:
print("checking condition")
first= False
target= (minute + 3) % 60
time.sleep(1)
更新:
我修改了代码,以便在每次迭代时对 localtime
进行一次调用,以完全确保分钟数不会在两次调用之间发生变化。
更紧凑但效率更低:
import time
while True:
minute= time.localtime().tm_min
if 'target' not in locals() or minute == target:
print("checking condition")
target= (minute + 3) % 60
time.sleep(1)