Dec-02-2020, 11:42 AM
One thing that I would do is create a timer event, rather than poll time.
This will free up the processor so other things can be done (if needed) while waiting for an event.
Try running this snippet:
example using threaded timer:
This will free up the processor so other things can be done (if needed) while waiting for an event.
Try running this snippet:
example using threaded timer:
import time, threading def threaded_timer(delay=1): print(f"In timer loop: {time.ctime()}") threading.Timer(delay, threaded_timer).start() # wake up after 6 seconds (can use decimal here for times less than 1 sec) threaded_timer(delay=4) print('Do something while waiting for timer') time.sleep(1) print('Do something else while waiting for timer')Notice that timer runs independently from other code.
Output:In timer loop: Wed Dec 2 06:42:13 2020
Do something while waiting for timer
Do something else while waiting for timer
In timer loop: Wed Dec 2 06:42:17 2020
In timer loop: Wed Dec 2 06:42:18 2020
In timer loop: Wed Dec 2 06:42:19 2020
In timer loop: Wed Dec 2 06:42:20 2020
In timer loop: Wed Dec 2 06:42:21 2020
...