'Continuous loop with while in Python in IIoT device

I found a solution for this online but just don't like it, see following code:

try:
    while True:
        # code here 
    except KeyboardInterrupt:
         break

The continuous is to be run in an IIoT device that has as OS Linux but with limited memory and capabilities. I could just do pip install keyboard to use any other characters than ctrl + c, let said "s" for stop but the package may not install in the device. I tested some code with curses to not avail. Please advice.



Solution 1:[1]

It might be possible that you have no other choice that get the keyboard lib. the other but very complicated solution is to use the serial

get the serial port your keyboard is connected to the IOT device. configure it with the right params and listen to messages. If you collect any type of message, it means you pressed a key

Here is the doc for pyserial

You can also try to compile the keyboard library with Cython to optimise space

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 floprm