等待指定时间的脚本

Script to wait till a specified time

等到指定时间

我需要暂停程序的最佳方法,直到提到指定时间(在变量 wait_time 中,小时、分钟、秒的元组)。我曾尝试为此使用 while loop。但是有没有一种更有效的方法,这样它就不会占用太多 CPU。等待之后,它执行一个名为 solve() 的函数。

wait_time=(12,10,15)
import time
time_str=time.localtime()
current_time=(time_str.tm_hour,time_str.tm_min,time_str,time_str.tm_sec)
while current_time!=wait_time:
    current_time=(time_str.tm_hour,time_str.tm_min,time_str,time_str.tm_sec)
else:
    print('Wait time over')
    solve()

对于内存部分,我需要一种比这更有效的方法。它应该等到系统时间是给定的时间。

我已经破解了一个应该适合你的方法:

timeWait.py
import time

def waitTime(hrs, mins, secs):
    totalTime = ((hrs * 3600) + (mins * 60) + secs) # this is the time to wait in seconds
    time.sleep(totalTime) # time.sleep() sleeps for a given number of seconds
Using it in the shell:
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeWait 
>>> timeWait.waitTime(1, 23, 42)
# This will wait/sleep for 1 hour, 23 minutes, and 42 seconds.
How you can use it in your program:
import time

def waitTime(hrs, mins, secs):
    totalTime = ((hrs * 3600) + (mins * 60) + secs)
    time.sleep(totalTime)

waitTime(12, 10, 15)
# The following code will run when the time is up
print('Wait time over')
solve()