I have a Python 3 script running on a Raspberry Pi (Buster) which writes some instrument data to my Nextion Display using the serial/UART interface. I have, for now, setup my code to sleep for 5 minutes after the current data are displayed. This working.
The Nextion Display is touch sensitive so that if I touch it, it will send a serial data string which can be read via my script and will tell me where on the screen it was touched.
Now, I would like to modify my code such that it will react to the touch-screen even during the sleep period. I could put the program into a tight loop instead of using time.sleep(300) and check the elapsed time and read the serial port during each loop. This sounds to me like I would be overworking the Pi and wasting CPU cycles. Is there a better way to pause certain sections of code while allowing other to continue?