0

I need to use a non blocking way to read from the console. I successfully managed to do this using select

ready = select.select(read_list, [], [], timeout)[0]
read_list = [sys.stdin]
timeout = 0.1 # seconds

My problem now is that I need to provide a text line (eg UI) before the input and would like the input cursor to be on the same line. Before when I was not using select I could achieve this by doing:

buff = raw_input('                     ENTER CODE: ------\b\b\b\b\b\b')

In this way the cursor would have been just after the comma (eg. on the first '-')

Now that I need to use stdin, the cursor always goes at the beginning of a new line. Even if I do:

print('                     ENTER CODE: ------\b\b\b\b\b\b\r')
while read_list:
        ready = select.select(read_list, [], [], timeout)[0]
        if not ready:
            idle_work()
        else:
            for file in ready:
                line = file.readline()
                if not line: # EOF, remove file from input list
                    read_list.remove(file)
                elif line.rstrip(): # optional: skipping empty lines
                    #treat_input(line)
                    buff =line.upper()
                    ETC...

Any ideas ?

2
  • 1
    Is there a particular reason that you are looking for a non-blocking input method? In my opinion, a better approach would be use another thread to do the background tasks, while let raw_input wait for the input. Commented Feb 10, 2014 at 12:11
  • yes. Im on a RaspberryPi (among various things) Commented Feb 10, 2014 at 12:13

1 Answer 1

2

I resolved this appending a ' at the end of the print statement and then flushing the std:

print('                     ENTER CODE: ------\b\b\b\b\b\b'),
sys.stdout.flush()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.