简体   繁体   English

等待使用datetimenow在Python 3中执行功能?

[英]Awaiting execution of a function in Python 3 by using datetimenow?

I'm tinkering with datetimenow() and I'm wondering how would you go about sort of "scheduling" an execution of a function when there are 2 dates ( now and future dates)? 我正在修改datetimenow()并且想知道当有两个日期( 现在将来的日期)时,您将如何“调度”函数的执行?

Basically I want to keep the programming running when the date (and time) is not the same, but when it is, call some function. 基本上,我想在日期(和时间)不同时保持程序运行,但是在日期(和时间)不同时,调用某些函数。

What I mean is: 我的意思是:

from time import sleep
import time
import datetime
timenow = datetime.datetime.now()

future = datetime.datetime(2018, 8, 19, 15,28)



while timenow != future:
    sleep(1.0)
if timenow == future:
    '''calling some function'''

Result: 结果:

Running this code only seems to keep looping although the minutes specified in future equals the minutes of timenow . 尽管future指定的分钟等于timenow的分钟,但运行此代码似乎只会保持循环。

You have two separate problems here, and you need to fix both. 您在这里有两个独立的问题,需要同时解决。


The first problem is that you never update timenow in your loop. 第一个问题是您永远不会在循环中更新timenow Whatever time it was when your program started, that's the value of timenow forever. 无论您的程序何时启动,这就是timenow永远的价值。

To fix that, you need to update it each time through the loop: 要解决此问题,您需要在循环中每次更新它:

while timenow != future:
    sleep(1.0)
    timenow = datetime.datetime.now()

The second problem is that if now is, say, 2018-08-19 15:27:59.123456 , that isn't equal to 2018-08-19 15:28:00.000000 . 第二个问题是,如果now等于2018-08-19 15:27:59.123456 ,则不等于2018-08-19 15:28:00.000000 And a second later, 2018-08-19 15:28:00.131313 also isn't equal to 2018-08-19 15:28:00.000000 . 和第二以后, 2018-08-19 15:28:00.131313 不等于2018-08-19 15:28:00.000000 There's about a 1 in a million chance that you happen to hit that microsecond exactly when you're only checking once per second. 当您仅每秒检查一次时,您碰巧碰到微秒的几率大约为百万分之一。

To fix that, just use < instead of != : 要解决此问题,只需使用<而不是!=

while timenow < future:
    sleep(1.0)
    timenow = datetime.datetime.now()

Now, 2018-08-19 15:27:59.123456 is less than the time you're waiting for—but 2018-08-19 15:28:00.131313 is not. 现在, 2018-08-19 15:27:59.123456小于您等待的时间,但2018-08-19 15:28:00.131313不是。 So, you don't miss it. 因此,您不会错过它。


While we're at it: 在我们的时候:

There's no need for that if statement at all. 根本不需要那条if语句。 You're just testing the opposite of the while condition. 您只是在测试while条件的相反条件。 But the only way you can get here is if the while condition is false, so the opposite condition will always be true, so why bother checking it? 但是,到达这里的唯一方法是,如果while条件为false,那么相反的条件将始终为true,那么为什么还要检查一下呢?

Also, if the only thing you're using timenow for is in that check, you don't even need to stick it in a variable; 另外,如果您仅使用timenow进行检查,则无需将其粘贴在变量中; just call now() directly. 只需直接调用now()

So: 所以:

from time import sleep
import datetime

future = datetime.datetime(2018, 8, 19, 15,28)

while datetime.datetime.now() < future:
    sleep(1.0)
'''calling some function'''

Meanwhile, there's really no reason to sleep for a second at a time. 同时,真的没有理由一次睡觉一秒钟。 Why not just sleep once, until future ? 为什么不睡一次,直到future呢? In some versions of Python, sleep can return early, so you still need a while loop, but you can still do this: 在某些版本的Python中, sleep可以提前返回,因此您仍然需要while循环,但是您仍然可以这样做:

from time import sleep
import datetime

future = datetime.datetime(2018, 8, 19, 15,28)

while datetime.datetime.now() < future:
    sleep((future - datetime.datetime.now()).total_seconds())
'''calling some function'''

Most of the time, this will sleep exactly once—occasionally maybe two or three times—instead of over and over again, so it won't, say, stop your laptop from going into low-power mode. 在大多数情况下,它只会休眠一次(有时可能是两到三次),而不是一遍又一遍,因此,它不会阻止笔记本电脑进入低功耗模式。

Also, there's a lot less chance that it'll wake up at, say, 2018-08-19 15:28:00.987654 instead of (very close to) on the second. 此外,它在例如2018-08-19 15:28:00.987654而不是在第二秒(非常接近)醒来的机会要少得多。 That may not matter for your use, but if it does matter, waking up on the second is surely the one you want. 这可能对您的使用无关紧要,但是,如果确实如此,唤醒第二个肯定是您想要的。

What you will need to do is specify how precise you want that equality to be. 您需要做的是指定您希望该等式达到的精度。 To do this you will specify a range of time that is acceptably considered to be "equal". 为此,您将指定一个可接受的“相等”时间范围。 See this SO post on an example of how to do that. 请参见 SO张贴关于如何做到这一点的例子。 You'll end up with something as straight forward as 您最终会得到与

if min_future_time < timenow and timenow < max_future_time:
  # Do something.

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

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