non-blocking IO EAGAIN on write

Thomas Guettler hv at tbz-pariv.de
Fri Jul 23 04:45:32 EDT 2010


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):
            check_timeout()
            chunk=fd.read(chunk_size)
            for i_eagain in range(10):
                try:
                    pipe.stdin.write(chunk)
                except IOError, exc:
                    if exc.errno==errno.EAGAIN:
                        logging.info('write to pipe %s EAGAIN. I will try again i=%s. %s' % (cmd, i_eagain, exc))
                        time.sleep(.3)
                        continue
                    logging.error('write to pipe %s failed: %s' % (cmd, exc), exc_info=True)
                    raise
                break # write was successful (default)
            else:
                raise Exception('Too many EAGAIN on write %s %s' % (cmd, exc), exc_info=True)

  Thomas


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de



More information about the Python-list mailing list