How to exit an input by timeout in Python

1

I'm making software in python running on raspberry, now it has a data entry on a matrix keyboard ( link ) and another by a bar code reader, which functions like a normal keyboard.

I loop through the menu first by listening to the matrix keyboard inputs, 1 for input in it and 2 for input by bar code .. what I need is the following, when I enter the type 2 that is the barcode reader, I use python input (), however I need a timeout for break this input if the user does not pass anything on the reader.

How do I get the timeout to exit an input in python?

    
asked by anonymous 19.05.2017 / 15:22

1 answer

0

You can use signal.alarm to implement a timeout or select.select

import sys, select
TIMEOUT = 10
i, o, e = select.select([sys.stdin], [], [], TIMEOUT)
if i:
    print("Você digitou: ", sys.stdin.readline().strip())
else:
    print('Você não digitou nada :(')
    
19.05.2017 / 16:42