Non-blocking login in python 3

I would like a cross-platform way to read input data from stdin in a non-blocking manner.

Something like this (how non-blocking sockets do it):

while True
    try:
        string = input("> ")
    except BlockingIOError:
        pass
    else:
        print(f"you typed {string}")

      

I am not asking about raw vs cooked mode. Ready mode is the default terminal behavior in which strings buffer stdin, raw mode makes characters available to the input stream as they are entered.

What I would like is a platform independent way to read from stdin and not block if there is no data to read. Again, I don't have to hit enter to make the characters stream available. I just don't want the program to stop completely when there is nothing in stdin.

I would also be happy with the asynchronous approach. Or something that throws a BlockingIOError like my example above. Or something like Java Scanner.hasNextLine (), which returns a bool representing whether stdin has data to read.

I would rather not use heavy weight solutions like threads or subprocesses.

+3
input python-3.x stdin nonblocking


source to share


No one has answered this question yet

Check out similar questions:

1829
Why is "1000000000000000 in range (1000000000000001)" so fast in Python 3?
1364
How do you read from standard input?
1146
How can I represent "Enum" in Python?
1028
What is the Python 3 equivalent "python -m SimpleHTTPServer"
949
How to remove border (outline) around text / input fields? (Chromium)
455
Non-blocking read on .PIPE subprocess in python
8
How to check if stdin is open without blocking?
4
Non-blocking readline for STDIN?
1
Change python stdin stream to windows console input
0
Non-blocking read byte from stdin



All Articles
Loading...
X
Show
Funny
Dev
Pics