Basically, I just want to wait for user input and echo it, though while the
user is not typing anything I'd like to have a background thread printing
"Come on, you're taking too long" or similar stuff.
The problem is that I can only read (and in batch) the background thread
printout messages on the console after feeding the raw_input function.
That's probably a fault of the Window's console handler, which
is blocking output while a read is active; the thread is likely trying
to do the output but the OS blocks.
On Windows, the msvcrt module is probably it. The documentation
does state that msvcrt.getch() /blocks/ if no key has been pressed, but
does not require an <enter> to complete. Since you don't want blocking
behavior, you will likely need to use msvcrt.kbhit()
A quick&dirty example, some parts could be cleaner, but it does
run...
-=-=-=-=-=-=-=-=-
E:\UserData\Dennis Lee Bieber\My Documents>script1.py
Type Q to quit
Well?... Type something
You entered the character 'a'
Well?... Type something
You entered the character 'D'
You entered the character 'F'
You entered the character 'D'
You entered the character 'E'
Well?... Type something
You entered the character 'd'
You entered the character 'e'
You entered the character 'n'
You entered the character 'n'
You entered the character 'i'
You entered the character 's'
You entered the character ' '
You entered the character 'l'
You entered the character 'e'
You entered the character 'e'
You entered the character ' '
You entered the character 'b'
You entered the character 'i'
You entered the character 'e'
You entered the character 'b'
You entered the character 'e'
You entered the character 'r'
Well?... Type something
Well?... Type something
You entered the character 'q'
E:\UserData\Dennis Lee Bieber\My Documents>
-=-=-=-=-=-=-=-=-
import msvcrt
import time
print "Type Q to quit"
i = 0
while True:
time.sleep(0.05)
if msvcrt.kbhit():
ch = msvcrt.getch()
else:
ch = None
if ch:
print "You entered the character '%s'" % ch
i = 0
else:
i += 1
if i == 40:
print "Well?... Type something"
i = 0
if ch == "q" or ch == "Q":
break
--