How to exit a timeout input in Python

Asked

Viewed 203 times

1

I’m making a python software running on Raspberry, today it has a data input by a keyboard Matrix (https://www.adafruit.com/product/419) and another by a barcode reader, which works like a normal keyboard.

I’m in infinite loop on the menu first listening to the Matrix keyboard inputs, 1 for entry into it and 2 for entry by barcode.. what I need is this, when I enter type 2 which is the barcode reader, I use the python input() but I need a timeout to break this input if the user does not pass anything in the reader.

How to timeout out an input in python ?

1 answer

0

You can use the Signal.Alarm to implement a timeout or 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 :(')
  • Tovmeod, I tried using Signal.Alarm, but it didn’t interrupt the input, it just said that I had typed up the timeout after I hit enter to exit the input.. now select it, whenever I try to use it I get the error: io.Unsupportedoperation: fileno

  • on my computer worked smoothly, Ubuntu with python 3.5. What you used?

  • can! I had to run as an administrator inside Raspberry’s Lxterminal.. in IDLE 2 or IDLE 3 it didn’t work..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.