HELP Non-Blocking reads from sys.stdin in Windows.

B

barr

Hi

I am in real need of a way to perform non blocking reads from sys.stdin on
windows. I have looked every where for an answer but but with no luck. I
beleive there there must be a way of doing this, can some one please help
asap.

Thanks in advance,

Barr
 
P

Paul Rubin

barr said:
I am in real need of a way to perform non blocking reads from sys.stdin on
windows. I have looked every where for an answer but but with no luck. I
beleive there there must be a way of doing this, can some one please help
asap.

Use a separate thread.
 
B

barr

hi
Do you mean something like the following.

class inputReader(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
buffer = sys.readline()

thread = inputReader()
thread.start()

Thanks

Kwame
 
M

Miki Tebeka

Hello Barr,
I am in real need of a way to perform non blocking reads from sys.stdin on
windows. I have looked every where for an answer but but with no luck. I
beleive there there must be a way of doing this, can some one please help
asap.
Warning: The below code wasn't tested at all...

-------------------------------
from Queue import Queue, Empty
from sys import stdin
from threading import Thread

# Reading from empty stdin error
class EmptyError(Exception): pass

# Input queue
_queue = Queue()

def produce():
'''Read one char at a time from stdin and place in _queue'''
try:
while 1:
c = stdin.read(1) # Read one char
if not c: # EOF
_queue.put(EOFError, 1)
break
_queue.put(c, 1)
except EOFError, e:
_queue.put(EOFError)

# Start the thread
t = Thread(target=produce)
t.setDaemon(1) # Don't inhibit interperter exit
t.start() # Start thread

def get():
'''Get one item from queue.

Might raise EmptyError if queue is empty or EOFError of end of input
'''
try:
val = _queue.get(0)
if val is EOFError:
raise EOFError
return val
except Empty:
raise EmptyError

def is_empty():
'''Tell if no input is ready'''
return _queue.empty()
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top