Python 如果现在时间在时间​​数组中则执行任务

Python execute task if time now is in array of times

我有这样一组时间:

array_of_times = ['2020-03-20 16:05:30', '2020-03-20 16:20:30', '2020-03-20 16:27:30', '2020-03-20 16:30:30', '2020-03-20 16:40:30', '2020-03-20 16:55:30', '2020-03-20 17:00:30', '2020-03-20 17:04:30', '2020-03-20 17:05:30', '2020-03-20 17:15:30', '2020-03-20 17:30:30', '2020-03-20 17:35:30', '2020-03-20 17:42:30', '2020-03-20 17:45:30', '2020-03-20 17:55:30', '2020-03-20 18:00:30', '2020-03-20 18:15:30', '2020-03-20 18:25:30', '2020-03-20 18:30:30', '2020-03-20 18:45:30', '2020-03-20 18:55:30', '2020-03-20 19:00:30', '2020-03-20 19:15:30', '2020-03-20 19:25:30', '2020-03-20 19:30:30', '2020-03-20 19:45:30', '2020-03-20 19:55:30', '2020-03-20 20:00:30', '2020-03-20 20:15:30', '2020-03-20 20:25:30', '2020-03-20 20:30:30', '2020-03-20 20:45:30', '2020-03-20 21:00:30', '2020-03-20 21:15:30', '2020-03-20 21:45:30', '2020-03-20 22:15:30']

而且我想知道 python 中是否有一种方法可以检查当前时间是否在数组中,如果是,则完成任务。

但是,我想以我的服务器不会过度工作只是等待时间等于数组中的时间的方式来执行此操作。

我尝试过简单的解决方案,例如:

now = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

while True:
    if now in array_of_times:
        print("Time is in")

然而,即使正确的时间过去了,它也从未对 if 语句求值为真。

我也试过使用:

for z in array_of_times:
    schedule.every().day.at(z).do(execute_task)
#given z was in the correct format

然而,还是没有运气。

感谢您的支持。谢谢

一个解决方案是修复 'now' 的分配。为了检查你的时间是否在数组中,你应该在循环中更新 'now'。

while True:
    now = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    if now in array_of_times:
        print("Time is in")

查找当前时间是否在你的数组中需要相对较长的时间,所以用时间列表做这样的事情不是一个好主意:

if now in array_of_times

这是因为它将搜索数组中的每个值,一次一个。

因此,更好的解决方案是有一组时间,因为散列会加快查找时间是否在数组中的时间。因此定义你的数组:

set_of_times = set(['2020-03-20 16:05:30', '2020-03-20 16:20:30', '2020-03-20 16:27:30', '2020-03-20 16:30:30', '2020-03-20 16:40:30', '2020-03-20 16:55:30', '2020-03-20 17:00:30', '2020-03-20 17:04:30', '2020-03-20 17:05:30', '2020-03-20 17:15:30', '2020-03-20 17:30:30', '2020-03-20 17:35:30', '2020-03-20 17:42:30', '2020-03-20 17:45:30', '2020-03-20 17:55:30', '2020-03-20 18:00:30', '2020-03-20 18:15:30', '2020-03-20 18:25:30', '2020-03-20 18:30:30', '2020-03-20 18:45:30', '2020-03-20 18:55:30', '2020-03-20 19:00:30', '2020-03-20 19:15:30', '2020-03-20 19:25:30', '2020-03-20 19:30:30', '2020-03-20 19:45:30', '2020-03-20 19:55:30', '2020-03-20 20:00:30', '2020-03-20 20:15:30', '2020-03-20 20:25:30', '2020-03-20 20:30:30', '2020-03-20 20:45:30', '2020-03-20 21:00:30', '2020-03-20 21:15:30', '2020-03-20 21:45:30', '2020-03-20 22:15:30'])

第三种替代解决方案是,如果您想最大程度地减少服务器正在做的工作量,首先对时间列表进行排序,查看下一次到达需要多长时间,然后使用time.sleep等待。 Python 的 time.sleep 不忙等待,所以您可以腾出 CPU。

from datetime import datetime
import time

array_of_times = ['2020-03-19 22:34:00']
array_of_times.sort()

while True:
    if not array_of_times:
        print("No more times in array.")
        break
    next_time_string = array_of_times[0]
    next_time = datetime.strptime(next_time_string, "%Y-%m-%d %H:%M:%S")  # get the datetime from the date
    del array_of_times[0]  # remove the first date
    time.sleep((next_time - datetime.now()).total_seconds())
    # do action
    print("I reached time:", next_time_string)

这段代码的输出是:

$ python test2.py
I reached time: 2020-03-19 22:34:00
No more times in array.