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

Discussion in 'Python' started by barr, Dec 28, 2004.

  1. barr

    barr Guest

    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
    barr, Dec 28, 2004
    #1
    1. Advertising

  2. barr

    Paul Rubin Guest

    "barr" <> writes:
    > 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.
    Paul Rubin, Dec 28, 2004
    #2
    1. Advertising

  3. barr

    barr Guest

    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
    "Paul Rubin" <http://> wrote in message
    news:...
    > "barr" <> writes:
    > > 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.
    barr, Dec 28, 2004
    #3
  4. barr

    Miki Tebeka Guest

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

    -------------------------------

    HTH.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.bizhat.com
    The only difference between children and adults is the price of the toys
    Miki Tebeka, Dec 28, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    640
    velle
    Jan 5, 2006
  2. Mark Dufour

    slow non-blocking reads

    Mark Dufour, Jun 29, 2006, in forum: Python
    Replies:
    1
    Views:
    370
    Lawrence D'Oliveiro
    Jul 2, 2006
  3. Mark Dufour

    Re: slow non-blocking reads

    Mark Dufour, Jul 3, 2006, in forum: Python
    Replies:
    1
    Views:
    315
    Lawrence D'Oliveiro
    Jul 5, 2006
  4. Antoon Pardon

    pep 3116 behaviour on non-blocking reads

    Antoon Pardon, Aug 9, 2007, in forum: Python
    Replies:
    1
    Views:
    256
  5. Stefano Sabatini
    Replies:
    6
    Views:
    286
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page