So, for example, you have Python code, for example:
file1.py
... do some stuff...
And at a certain point in the document that you want to always check for input:
while True:
input = raw_input(">>>")
... do something with the input...
It will always wait for input. You can sink this endless loop as a separate process and do other things at the same time, so user input can have an effect on the tasks you perform.
, ( ActiveState Steven D'Aprano), , , .
import sys
try:
import tty, termios
except ImportError:
try:
import msvcrt
except ImportError:
raise ImportError('getch not available')
else:
getch = msvcrt.getch
else:
def getch():
"""getch() -> key character
Read a single keypress from stdin and return the resulting character.
Nothing is echoed to the console. This call will block if a keypress
is not already available, but will not wait for Enter to be pressed.
If the pressed key was a modifier key, nothing will be detected; if
it were a special function key, it may return the first character of
of an escape sequence, leaving additional characters in the buffer.
"""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
? , getch() , . :
while True:
getch()
input = raw_input("Enter input")
do_whatever_with_it
.
, Python 3.x raw_input, ().