non-blocking IO EAGAIN on write

Kushal Kumaran kushal.kumaran+python at gmail.com
Sat Jul 24 00:32:08 EDT 2010


On Fri, Jul 23, 2010 at 2:15 PM, Thomas Guettler <hv at tbz-pariv.de> wrote:
> Hi,
>
> I use non-blocking io to check for timeouts. Sometimes I get EAGAIN (Resource temporarily unavailable)
> on write(). My working code looks like this. But I am unsure how many bytes have been written to the
> pipe if I get an EAGAIN IOError. Up to now I retry with the same chunk.
>
> If I get EAGAIN can I just sleep, and then retry with the same data chunk?
>
> pipe=subprocess.Popen(cmd, stdin=subprocess.PIPE, bufsize=-1)
> fcntl.fcntl(pipe.stdin, fcntl.F_SETFL, os.O_NONBLOCK)
> ....
> chunk_size=1024
>        while select.select([], [pipe.stdin], [], 5):

Please read the documentation of the select.select function.  You are
not using it correctly.  After the call returns, you need to check
whether the descriptor you are interested in is actually ready.  If
select reaches your timeout, it will simply return three empty lists,
and your write will get EAGAIN.  In general, after select has told you
a descriptor is ready, the first write after that should always
succeed.

BTW, from the documentation of file objects, it seems write always
wants to write the entire string you give it, since it does not return
anything.  In that case, you might want to get the descriptor for the
file object (using pipe.fileno()) and use that for writing.

> <snipped remaining code>

-- 
regards,
kushal



More information about the Python-list mailing list