import sys
import threading
import tty
import termios
def loop():
fd = sys.stdin.fileno()
mode = termios.tcgetattr(fd)
tty.setraw(fd)
try:
while True:
sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, mode)
threading.Thread(target=loop).start()
I tried to manually send End-Of-Transmission character using sys.stdin.write(chr(4))
but it results in io.UnsupportedOperation: not writable
.
I tried to close stdin but it will not terminate blocking read(1) method immediately. It triggers an exception after I typed another key.
I tried seletors but selector.select() need extra keys after I invoked selector.close()
. And selector it self needs an event loop and this loop becomes uninterruptable.