[Tutor] reading from stdin
Peter Markowsky
peterm at ccs.neu.edu
Wed Mar 2 00:15:19 CET 2005
Hi,
On Mar 1, 2005, at 6:01 PM, Sean Perry wrote:
> jfouhy at paradise.net.nz wrote:
>> If I do:
>> $ ./produce.py | ./read.py
>> I get nothing for ten seconds, then I get the numbers 0 through 9,
>> one per line.
>> What am I missing?
>
> From the python man page:
> -u
> Force stdin, stdout and stderr to be totally unbuffered. On
> systems where it matters, also put stdin, stdout and stderr in binary
> mode. Note that there is internal buffering in xreadlines(),
> readlines() and file-object iterators ("for line in sys.stdin") which
> is not influenced by this option. To work around this, you will want
> to use "sys.stdin.readline()" inside a "while 1:" loop.
>
What exactly is the "right" way to put sys.stdin in unbuffered mode?
in another program I've tried to accomplish that by doing something
like this
import os
import fcntl
<...>
stdinfd = sys.stdin.fileno()
fcntl.fcntl(stdinfd, fcntl.F_SETFL, os.O_NONBLOCK)
...
and then calling later
data = sys.stdin.read(numbytes)
is this correct?
-Pete
More information about the Tutor
mailing list