Search code examples
pythonselectsys

python Keyboard input with timeout? getting error OSError: [WinError 10038] An operation was attempted on something that is not a socket


I am using windows and python 3.8. I want to send additional input into the python script I also want a timeout on the input.

I tried the following:

import sys, select

print("You have 5 seconds to answer")

i, o, e = select.select([sys.stdin], [], [], 5)

if (i):
    print("You said", sys.stdin.readline().strip())
else:
    print("You said nothing")

Sources: Stackoverflow and thewebdev

This resulted in an error:

i, o, e = select.select([sys.stdin], [], [], 5)

OSError: [WinError 10038] An operation was attempted on something that is not a socket

How would I get timeout for a input working?


Solution

  • The solution I found is:

    $ pip install inputimeout

    from inputimeout import inputimeout, TimeoutOccurred
    try:
        something = inputimeout(prompt='>>', timeout=10)
    except TimeoutOccurred:
        something = 'Time is up'
    print(something)