0

Accepted solution: Python: How to get input from console while an infinite loop is running? from timgeb

I have been searching for an answer to this for two months on and off. I am taking on a rather large undertaking with the project I am currently working on, and eventually would like the script to output something to the terminal whilst allowing the user to enter commands.

The only solution that I have thought of thus far, is to implement curses with two 'windows', allowing keyboard interrupts via getch() to print characters to the screen in a manner that would parse each key press and print the character to the second window. I would do this in a while loop, allowing the script to function as normal while allowing the user to enter commands, simply adding the characters from each keypress to a string, and once enter is pressed resolve the entered text.

If there is a better what to do this than the amount of scripting the curses process would require that would be great! I have no idea of what the process for that should be, though (I don't even know if my curses idea would work). I have The Python Standard Library by Example, and noticed a section on multiple processes working together, is this what I should be looking into?

Sample:

def message():
    print "Writing to screen during raw_input() pause."

ipt = raw_input('>>> ')    #get input from user

# execute message() here even if user has not entered any information

Terminal:

"Writing to screen during raw_input() pause."
>>>

Ideally it would print output to the screen above the input line.

Thanks!

Community
  • 1
  • 1
spectre-d
  • 509
  • 4
  • 14
  • 7
    It's unclear to me what you are trying to achieve. Clearly, it can't just be asking for the input in a separate thread, because otherwise you would have done that? How would a typical session where the user inputs something look like and what functions should be executed during that time? – timgeb Feb 26 '16 at 13:12
  • 2
    Welcome to Stack Overflow. Your question doesn't provide us with enough detail. At a minimum, you should post a relevant sample of your code. I encourage you to take the [tour](http://stackoverflow.com/tour) and consult the [help center](http://stackoverflow.com/help) for guidance on asking good questions. – McMath Feb 26 '16 at 13:15
  • 1
    Does [this](http://stackoverflow.com/questions/24000455/python-how-to-get-input-from-console-while-an-infinite-loop-is-running) or [this](http://stackoverflow.com/questions/23892216/key-press-and-wait-for-a-fixed-amount-of-time) help? – timgeb Feb 26 '16 at 13:17
  • 1
    "Ideally it would print output to the screen above the input line. " <- then just execute `message` before `raw_input`? – timgeb Feb 26 '16 at 13:29
  • That would work if I didn't want it to execute during raw_input(). I will look into threading, it looks like that's exactly what I am looking for, thanks timgeb. – spectre-d Feb 26 '16 at 13:35

0 Answers0