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

Miki Tebeka miki.tebeka at
Tue Dec 28 21:38:06 CET 2004

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'''
        while 1:
            c = # Read one char
            if not c: # EOF
                _queue.put(EOFError, 1)
            _queue.put(c, 1)
    except EOFError, e:

# 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
        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()


Miki Tebeka <miki.tebeka at>
The only difference between children and adults is the price of the toys

More information about the Python-list mailing list