Non-blocking raw_input

  • Thread starter Jorge Louis de Castro
  • Start date
J

Jorge Louis de Castro

Hi,

Could anyone tell me whether I can find a non blocking alternative to
raw_input that works on windows? Is there not a python way of achieving
this? Can I find it somewhere in the documentation?
Any help will be highly appreciated

Cheers
j.
 
P

Peter Hansen

Jorge said:
Could anyone tell me whether I can find a non blocking alternative to
raw_input that works on windows? Is there not a python way of achieving
this? Can I find it somewhere in the documentation?
Any help will be highly appreciated

Depending on what your requirements are (since you don't really say),
the functionality in the standard msvcrt module might help.
 
F

Fredrik Lundh

Jorge said:
I have indeed tried the msvcrt module but none of the examples given works
as described on a XP+SP2 box.

what examples? how did you run the examples?

(the keyboard interface functions in msvcrt only work if the program's attached to
a Windows console. if you run the program under an IDE, that may not be true).

</F>
 
D

Dennis Lee Bieber

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

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top