473,378 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Non-blocking raw_input

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
3 6385
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.