By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Non-blocking raw_input

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jorge Louis de Castro wrote:
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.
Jul 19 '05 #2

P: n/a
Jorge Louis de Castro wrote:
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>

Jul 19 '05 #3

P: n/a
On Tue, 28 Jun 2005 10:29:35 +0000, "Jorge Louis de Castro"
<jo****@hotmail.com> declaimed the following in comp.lang.python:
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

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.