【问题标题】:Python: Reset a timer after completing what was desiredPython:完成所需内容后重置计时器
【发布时间】:2021-06-08 13:09:43
【问题描述】:

我在寻找知识。

我目前正在开发一个程序,如果我的传感器在 30 秒内没有检测到任何人,则会启动一个视频。我目前正在使用time.time(),但令人担忧的是,尽管尝试重置,但计时器仍在继续,并且在视频结束后不会重置为 0。

所以我尝试了一些新的研究,发现了一个关于 time.clock() 的话题,我最终得到了这个:Python's time.clock() vs time.time()

但我不确定我是否完全理解timeittime.process_time()time.perf_counter() 的作用,有没有可能以我想要的方式使用它们?还是仅用于计算几个示例中的代码的执行时间?

精度:

当我达到 30 秒时,我输入 if 启动视频,启动视频,然后是 time.sleep()。视频运行时计时器不会暂停并继续。视频完成后,我尝试重置计时器,一旦达到 50 秒但我做不到,我可能不会重置我需要的东西。因此,计时器永远不会停止,并且我的其他需要计时器的功能(if timer> 7)在条件满足后直接启动(而进入其他功能时它们应该从 0 重新开始)

PS:当一个视频在运行时,其他视频不叠加,请耐心等待

while True:
    if ...:
        try:
            if(200 <= DD): # If we are more than 200 cm away
                # We count the number of seconds between the last checkpoint and now
                face_walking_apparition_time = time.time() - walking_timer # (t+3) - t = 3, it's how u calculate timer
                
                # Then we check if a face has appeared for more than 30 seconds:
                if face_walking_apparition_time > 30: # If the block is detected more than 30 seconds
                    # Display video
                    time.sleep(25)
                    face_motionless_apparition_time = 0 # I try to reset here
                    face_walking_apparition_time = 0 # I try to reset here
                if face_walking_apparition_time >= 50: # After 20 seconds of video, reset
                    face_walking_apparition_time = 0 #I try to reset here
                    face_motionless_apparition_time = 0 # I try to reset here
                    
            elif (50 < DD) and (DD < 200):
                # As we don't see no face, we have to reset our checkpoint to "now"
                face_motionless_apparition_time = 0 # I try to reset here   
                face_walking_apparition_time = 0 # I try to reset here

PS:我是 python 新手,所以我的代码可能很难读

【问题讨论】:

  • 我不确定您的问题是什么,您能否更准确地说(例如哪个计时器导致问题)?是否也可以隔离直接相关的代码块?
  • time 不是定时器,time 是时间相关函数的库。 time.time() 是现在的当前时间,你不能暂停时间的进程,即使是 python。
  • @JeffUK 那么有没有办法创建一个可以满足我期望的计时器? (进行了新的编辑)

标签: python time reset


【解决方案1】:

我想你可能误解了“计时器”的工作原理

我们通常做的是用time.time()初始化一个变量,它表示自1970年1月1日以来经过的秒数,然后当我们想像计时器一样测量时间时,我们减去time.time()的第二个实例与前一个,这为我们提供了两者之间的测量时间。

使用time.sleep(n) 只是在进入下一行之前等待 n 秒,所以它不会重置你的计时器

您需要围绕这些概念重新考虑您的代码

【讨论】:

    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2022-06-21
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多