Python: 完成所需内容后重置计时器

Python: Reset a timer after completing what was desired

我正在寻找知识。

我目前正在开发一个程序,如果我的传感器在 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() 初始化一个变量,它表示自 01/01/1970 以来经过的秒数,然后当我们想像计时器一样测量时间时,我们减去第二个实例time.time() 与前一个相比,这给了我们两者之间的测量时间。

使用 time.sleep(n) 只需等待 n 秒再转到下一行,因此它不会重置您的计时器

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