Help a newbie use Python's OS module:

Donn Cave donn at u.washington.edu
Wed Oct 11 20:03:55 EDT 2000


Quoth Kevin O'Gorman <kogorman at pacbell.net>:
...
| I must admit, though, that I didn't quite understand the comment
| about no using file objects.  Doesn't it get cumbersome to
| direct output to the raw descriptors?  What do you use?
| I didn't see anything like fprintf, or a way to set sys.stdout
| to a descriptor (a bare int doesn't work).

Well, sys.stdout is a file object.  File objects really can be very
useful, depending on your application, and I guess very generally
speaking that means very useful for most applications.  In a few
applications they do cause more problems than they solve.  It sometimes
seems to me that people insist on using them even so, and that's
why I mention it.

A file object is basically a buffer.  With this buffer, it can do
things like read a line of data, when the device returns data in
arbitrary amounts that don't coincide with line boundaries, or write
a few bytes of data at a time, and efficiently write the data to the
device in larger blocks.

That buffer is in your process space, not the external world the
device represents, so other processes or even the kernel can't see it.
If you call select() to ask whether you have more data, it looks at
the device and doesn't know about data in your process buffers.
If another process is reading at the other end of the pipe, or a
socket, it can't see data in your process buffers.

You can defeat the write buffer with flush().  Some people try to
defeat the read buffer by opening the file with no buffering, but
this can bring on a lot of 1-byte system reads that degrade performance.

Direct device I/O is easy enough that these workarounds may not be
worth the trouble.  Which is more cumbersome,
  os.write(fd, 'This I/O will be effective immediately.\n'),
or
  fp.write('This I/O won't reliably happen until flushed or closed.\n')?

In the worst case, you may find yourself implementing a subset of the
stdio buffering, in order to do something like readline() and account
for the actual state of the device (stdio doesn't support direct access
to its buffers, so you don't know what's in there.)

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list