readline() blocks after select() says there's data??

Donn Cave donn at u.washington.edu
Fri Mar 15 13:19:33 EST 2002


Quoth grante at visi.com (Grant Edwards):
| In article <mailman.1016212173.16075.python-list at python.org>, Rich Cook wrote:
...
|> So do I have to read the data byte by byte using read(1) and
|> examine each character to avoid blocking? Is there any way to
|> find out how much data is actually waiting for me in the file
|> object so I can read it in bigger increments, or must I call
|> select then read(1) for every byte?
|
| You don't have to read it 1 byte at a time. Just call
| f.read(1024) and it will give you however many bytes are
| available, up to the max of 1024.  You can use a max different
| than 1024 if you want.

I think you're making the problem worse here.

  import popen2
  t = popen2.Popen3('echo L1; sleep 2; echo L2; sleep 2; echo L3')
  print 'First read: ', repr(t.fromchild.read(1024))

  $ python /tmp/x
  First read:  'L1\nL2\nL3\n'

C I/O basically sucks for this application.  When you ask for 1024,
fread is bound to get that amount, just as it is if you ask for a
line via fgets ("readline".)  If you omit the amount, you're asking
for all the data, won't return until the socket's other end closes.
If you read byte by byte, you won't know when to stop, because select
doesn't look at stdio I/O buffers and after the first byte all the
existing data was probably read up from the device, so select won't
see anything there.

Rich, I thought Grant identified your real problem (calling select
wrong) in his first followup.  I added some comments relevant to
the above issues in other followups.  Did you miss all that stuff?

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list