I have a python app that runs a loop continuously.
XUBUNTU 20.04.
Python 3.8
To calculate the elapsed time, I use the time module. End of the loop, app sleeps to ensure the loop interval is constant. Here is a sample:
import time
scan_time = 0.1
while True:
tic = time.time()
do_work1()
toc = time.time()
et = toc - tic
print(f"Elapsed time for Work1 was {et}")
do_work2()
toc = time.time()
et = toc - tic
print(f"Overall elapsed time was {et}")
if et < scan_time:
time.sleep(scan_time - et)
I had complaints from users that the app hangs from time to time. Once I observed that the
print(f"Overall elapsed time was {et}")
had printed a large negative number (equal -6124.428).
That means the sleep time was a large positive number and indeed the app had fallen to a deep sleep.
Why on earth would the et become negative?
Thanks