How to get number of bytes written to nonblocking FIFO when EAGAIN is raised?

Adam Skutt askutt at gmail.com
Tue Jul 19 23:15:15 EDT 2011


On Jul 19, 9:19 pm, Aaron Staley <usaa... at gmail.com> wrote:
> However, if interpreter 1 overfills the FIFO, we get an error (EAGAIN)>>> f.write('a'*70000)
>
> IOError: [Errno 11] Resource temporarily unavailable
>
> However interpreter 2 still receives data>> len(f.read())
>
> 65536
>
> It looks like interpreter 1 pushed data until the FIFO was full and
> then raised the IOError.  Interpreter 2 constantly received some, but
> not all, of what interpreter 2 tried to send.
> Unfortunately, the IOError seems to have no attribute indicating how
> much data was successfully sent.  I've looked through the docs and
> can't seem to figure out how; can anyone land some advice?

You need to do as Roy Smith suggested and use the actual OS I/O calls
os.read() and os.write().  os.write() returns the number of bytes
actually written to the underlying descriptor.  Python file objects
are akin to FILE structures in C: they perform buffering and other
operations internally that makes them less than suitable for usage
with asynchronous UNIX I/O. In particular, they buffer I/O internally
before writing it to the descriptor, so there's no direct relationship
between calling file.write() and how much data is written to the
stream. In addition, file objects also simply raise the underlying OS
error when it occurs.  The UNIX write(2) syscall assumes that you have
been keeping track of how many bytes you've successfully written to
the stream and does not track it for you.

Adam




More information about the Python-list mailing list