每3分钟检查一次条件,无功能,而不会中断循环

发布于 01-17 09:12 字数 843 浏览 1 评论 0原文

我有这个工作代码,考虑到当地时间,每 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)

但它不起作用:它什么也不做。 有什么想法吗?

I have this working code that checks a conditions every 3 minutes considering the local time, so every 0, 3, 6, 9.....It prints "checking condition".

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)

The problem is that I need the code without functions, so I need to find a way to write this code without using any funcion, and I cannot use break or interrput the loop

I tried:

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)

But it does not work: it does not do nothing.
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑着哭最痛2025-01-24 09:12:06

您可以在一个语句中压实该函数:

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)

you can compact the function in one statement:

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)
等风来2025-01-24 09:12:06

第一次,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)

The first time, the get_next_time() will only be executed when next_run == now.tm_min. The second time, you execute it each loop

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)
寄人书2025-01-24 09:12:06

到达下一个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)

Rounding to the next multiple of 3 minutes contradicts the specification "every 0...".

It is enough to do

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)

Update:

I modified the code so that a single call to localtime is made on every iteration, to make fully sure that the minutes do not change between the calls.

More compact but less efficient:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文