简体   繁体   English

Python检查分钟是否已通过

[英]Python Check if a Minute has passed

Im running part of a script just once per minute and came up with this: 我每分钟只运行一次脚本的一部分,并想出了这个:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute >= 0):
        return True
    else:
        return False

Problem here is that if the minute is 59 it runs everytime until its past that time - its not much of a bother performance wise for me. 这里的问题是,如果分钟是59,那么它每次都会运行,直到它过去那段时间 - 这对我来说并不是一件好事。 But I still dont like it happening! 但我仍然不喜欢它发生!

I thought of something like this now: 我现在想到这样的事情:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute >= 0 and ran == false):
        ran = True
        return True
    else:
        return False

Then on another part of the script I turn ran false again when the minute is != 59 and the variable isnt already false - but that seems crude? 然后在脚本的另一部分,当分钟是!= 59并且变量不是假的时候,我再次变为假 - 但这看起来很粗糙?

On another note - is there any other way to check if a minute has passed? 另一方面 - 有没有其他方法来检查一分钟是否过去了? Maybe im making things complicated ... 也许我让事情变得复杂......

Edit: Maybe I was not clear enough: 编辑:也许我不够清楚:

  1. Run only ONCE per minute. 每分钟仅运行一次ONCE。
  2. Execution time varies by many seconds but takes less then 30s. 执行时间变化很多秒,但需要不到30秒。

Im looking at timedelta now. 我现在正在看timedelta。

Don't work with minutes like that; 不要像这样的分钟工作; if the time is, for example, 00:00:59, your code will believe a minute has passed the very next second. 如果时间是,例如00:00:59,您的代码将相信一分钟已经过了下一秒。

Instead, use something like time.time() , which returns seconds passed since the epoch: 相反,使用类似time.time()东西,它返回自纪元以来传递的秒数:

def minute_passed(oldepoch):
    return time.time() - oldepoch >= 60

That can still be off by almost a second for the same reasons, but that's probably more acceptable. 由于同样的原因,这仍然可能会被关闭几秒钟,但这可能更容易被接受。

You can use seconds since epoch to get the time in seconds so you don't have to worry about minutes wrapping around: 您可以使用自纪元以来的秒数以秒为单位获得时间,这样您就不必担心会议时间:

import time
oldtime = time.time()
# check
if time.time() - oldtime > 59:
    print "it's been a minute"

Use time.sleep : 使用time.sleep

from time import sleep

while True:
    # do something
    sleep(60)

I think you'll find it much easier to use the time() function in the time module, which returns the number of seconds elapsed since the 'epoch'. 我想你会发现在time模块中使用time()函数要容易得多,它会返回自'epoch'以来经过的秒数。

import time

oldtime = time.time()

def minutePassed(oldtime):
    currenttime = time.time()
    if currenttime - oldtime > 60 and ran == False:
        ran = True
        return True
    else:
        return False

I had not much success with timedelta so I went with my former crude idea: 我在timedelta上并没有太大的成功,所以我选择了我以前粗略的想法:

def minutePassed(oldminute):
    currentminute = time.gmtime()[4]

    if ((currentminute - oldminute) >= 1) or (oldminute == 59 and currentminute == 0):
        return True
    else:
        return False

Then in the script: 然后在脚本中:

while True:
    dosomething()

    if minutePassed(time.gmtime(lastrun)[4]):
        domore
        lastrun = time.time()

This works perfectly fine for me now - only runs once a Minute - sleeping or anything is a bad choice for me here since the execution time on each loop is unreliable. 这对我来说完全没问题 - 只运行一次 - 分钟 - 睡觉或任何事情对我来说都是一个糟糕的选择,因为每个循环的执行时间都不可靠。

I had the exact same problem and I think I found a solution: 我有完全相同的问题,我想我找到了一个解决方案:

import time


oldtime = time.time()
print oldtime

while oldtime + 60 != time.time():

    if oldtime + 3 == time.time():
        print "60 secs passed"
        print time.time()
        break

You can delete the print statements, I had them there just to see if it works, hope it's what you are looking for. 你可以删除打印语句,我把它们放在那里只是为了看它是否有效,希望它是你正在寻找的。

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

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