我想在各种时间安排一个文本'hello world'(形成一个列表),因为脚本已经启动。什么是最好的方法来做到这一点?在Python中启动脚本之后的特定时刻计划打印命令
这可能是错的,但我到目前为止有:
import time
times = [1.76493425, 3.10174059, 4.49576803, 10.99379224, 18.84178369] #at these times since onset of script, a text "hello world" should be printed
start = time.time()
def main():
for cuetime in times:
print ('hello world', ' - timing: ', cuetime, ' - now: ', time.time()- start)
yield viztask.waitTime(cuetime)
main()
这给了我:
('hello world', ' - timing: ', 1.76493425, ' - now: ', 0.0)
('hello world', ' - timing: ', 3.10174059, ' - now: ', 1.7699999809265137)
('hello world', ' - timing: ', 4.49576803, ' - now: ', 3.5379998683929443)
('hello world', ' - timing: ', 10.99379224, ' - now: ', 5.305999994277954)
('hello world', ' - timing: ', 18.84178369, ' - now: ', 7.075000047683716)
但我真正需要的是时间要素/项目是一样的“现在”时间,因为时间列表中的元素是相对于剧本开始应该打印文本“hello world”的时间。
感谢quapka的方式,这是优秀的,特别是评论,这使它透明。 – Spica
我很高兴它有帮助。有改进它的空间。每次可能传递'start'。出于某种原因,我想到了装饰器和背景管理器。但是,如果我想这些工作更复杂,那将会很有趣。 – quapka