python - How to check potentially empty stdin without waiting for input? -


I am trying to read from the keyboard input without waiting for input. Its purpose is used in the "infinite" loop ala, while true:.

So far I have been trying to manipulate the Library, but there is no luck. Although it does not wait to enter, it is still waiting for some input. I do not want to wait for this input, but if there is no input then simply check "" and come back or some placeholder .

Here's what I'm doing: <> def readchar (): try fd = sys.stdin.fileno () old_settings = termios.tcgetattr (FD): tty.setraw (sys.stdin.fileno ()) ch = sys.stdin.read (1) finally: termios.tcsetattr (fd, termios.TCSADRAIN, old_settings) returns CH Diif main (): while right: current = readchar () If present == "few letters": print ("things happen") POSIX I / O functions that are finally contained in Python's file objects, in two different modes, blocking and non- Blocked, controlled by nominated flag. Specifically, the function says:

?? When attempting to read a file. That one ?? | Is there any data currently availableĆ¢ ?? | Ć¢, then O_NONBLOCK is set, will return () -1 and not error to [EAGAIN] .

In Python, this flag is available in the module.


sys.stdin is already open, so you can just type the O_NONBLOCK os.open function , Think ?? | What do you do? OK, you really want to probably open / dev / tty instead; In that case, the answer is clear, depending on how it really is. But suppose you do not do this. Therefore, you already want to change the open file's flag. That's exactly what this is. You can use the F_GETFL operation in the bit to read the current flag or for the O_NONBLOCK , and F_SETFL result. If you want to restore things, then you can remember the latter flag.

In Python, the fcntl function, and operation constant, are available in the module.


One last issue: sys.stdin is not a raw file object, it is one which connects buffering up which gives itself at the top of the Unicode decoding a . So, sys.stdin.read () is not directly calling posix. read function. To do this, you need to use sys.stdin.buffer.raw . And if you want to go back and forth between raw and normal input, you may have to do very careful flushing (note that this means that you are leaving Unicode and instead of a single byte bytes < / Code> object, which can say, half of a UTF-8 or one fourth of the terminal escape character. Hopefully you are expecting this.)


Now, FileIO.read returns what happens Is there nothing available when? Well, this is the implementation of the rayobase , and says:

If 0 bytes are back, and the size is not 0, then this indicates the end of the file If the object is in non-intercept mode and no byte is available, then no has returned.

>

Therefore, put all this together:

  old_settings = termios tcgetattr (FD) old_flags = fcntl.fcntl (FD, fcntl.F_GETFL) tried: tty.setraw (FD) fcntl.fcntl (FD, fcntl.F_SETFL, old_flags | os.O_NONBLOCK) return sys.stdin.buffer.raw. read (1) finally: fcntl.fcntl (FD, fcntl.F_SETFL, old_flags) termios.tcsetattr (FD, termios.TCSADRAIN, old_settings)   

one last thing: what Is there any way to tell that the input is ready without reading it? Yes, but not portable, depending on your platform, select , poll , epoll , and / or kqueue Can be available and possibly work on to read (0) to return none instead of b Can be guaranteed. And so on. You can read your local man page. But a simple solution does the study of C: add a 1-byte-max buffer, and use it to read your own read and peek or Do and unread wrappers.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -