在 python 中从初始定义的时间后延迟命令
Delay commands after defined time from initial in python
我正在尝试从初始执行时间开始延迟命令。换句话说,我想要一个开始时间点,其余命令将参考该开始时间点延迟。示例:
print("start")
# command that delays for a minute since the start
print("it's been a 1 min")
# command that delays for n minute since the start
print("it's been n min")
我正在尝试从 sleep()
切换,但没有找到适合我需要的东西。感谢任何帮助。
import time
initial_time = int(time.time()) #frame of reference in seconds
def delta_sleep(s):
"""
Parameters:
s: seconds since elapsed to sleep until
"""
if int(time.time()) > initial_time + s:
# check if the delta time has already passed
return
else:
# find time needed to sleep to reach the specified param 's'
needed_sleep = (initial_time+s) - int(time.time())
time.sleep(needed_sleep)
# example
print("The program has started")
delta_sleep(5)
print("Five seconds have passed")
delta_sleep(8)
print("8 seconds have passed")
delta_sleep(1) # does not sleep at all
我正在尝试从初始执行时间开始延迟命令。换句话说,我想要一个开始时间点,其余命令将参考该开始时间点延迟。示例:
print("start")
# command that delays for a minute since the start
print("it's been a 1 min")
# command that delays for n minute since the start
print("it's been n min")
我正在尝试从 sleep()
切换,但没有找到适合我需要的东西。感谢任何帮助。
import time
initial_time = int(time.time()) #frame of reference in seconds
def delta_sleep(s):
"""
Parameters:
s: seconds since elapsed to sleep until
"""
if int(time.time()) > initial_time + s:
# check if the delta time has already passed
return
else:
# find time needed to sleep to reach the specified param 's'
needed_sleep = (initial_time+s) - int(time.time())
time.sleep(needed_sleep)
# example
print("The program has started")
delta_sleep(5)
print("Five seconds have passed")
delta_sleep(8)
print("8 seconds have passed")
delta_sleep(1) # does not sleep at all