[英]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
然而,仍然沒有運氣。
任何支持表示贊賞。 謝謝
一種解決方案是修復“現在”的分配。 為了檢查您的時間是否在數組中,您應該在循環中更新“現在”。
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.