[PYTHON] Timer used in while loop

Thing you want to do

I made a timer for making a low temperature cooker. In the style, I thought it would be better to use threading, but I made it quickly within my knowledge.

It is supposed to be used in a while loop, and it keeps returning True if it is within the set time, and returns False if it exceeds the set time. The timer sets the interval and processing time in advance and runs it.

Shortening the interval time improves the accuracy of the processing time.

The accuracy required for the processing time varies depending on the application, so we made it variable.

In the case of low temperature cooking, the processing time is often several hours, so it is not necessary to rotate it at very short intervals.

python


import sys
import time

print('How many hours do you want to stop?')
hour = float(input())
set_time = hour * 60*60
print(str(set_time) + 'I'll stop after a second')

print('Count interval')
interval = float(input())
print(str(interval) + 'Count at sec intervals')

class Countdown_Timer:
    def Timer(self,t):
        time_now = time.time()
        delta_time = time_now - start_time
        while delta_time < t:
            time_now = time.time()
            delta_time = time_now - start_time
            time.sleep(interval)
            flag = True
            return flag
        else:
            global time_measured
            time_measured = delta_time
            flag = False
            return flag


timer = Countdown_Timer()
start_time = time.time()

print('start Timer')
while timer.Timer(set_time):  
#    print('Timer On')
    pass
else:
    print('Timer Off')
    print('the time is' + str(time_measured)+'It was seconds.')
    print('The error is' + str(time_measured - set_time) + 'Seconds.')

In low-temperature cooking, the processing time is often set in hours, so the processing time is input in hours.

When you start it, it looks like this.

I want to eliminate the echo of input ().

How many hours do you want to stop?
0.002
7.199999999999999 I'll stop after a second
Count interval
0.3
0.Count at 3sec intervals
start Timer
Timer Off
Time is 7.It was 243600845336914 seconds.
The error is 0.04360084533691477 seconds.

Recommended Posts

Timer used in while loop
Implement timer function in pygame
Settings often used in Jupyter
Used in machine learning EDA
Frequently used commands in virtualenv
A timer (ticker) that can be used in the field (can be used anywhere)
8 Frequently Used Commands in Python Django