write_nonblock(p1) public

Writes the given string to ios using the write(2) system call after O_NONBLOCK is set for the underlying file descriptor.

It returns the number of bytes written.

write_nonblock just calls the write(2) system call. It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result may also be smaller than string.length (partial write). The caller should care such errors and partial write.

If the write buffer is not empty, it is flushed at first.

When write_nonblock raises EWOULDBLOCK, write_nonblock should not be called until io is writable for avoiding busy loop. This can be done as follows.

begin
  result = io.write_nonblock(string)
rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EINTR
  IO.select(nil, [io])
  retry
end

Note that this doesn’t guarantee to write all data in string. The length written is reported as result and it should be checked later.

Show source
Register or log in to add new notes.