Wraps a Unix
file descriptor fd
in an Lwt file_descr
fd'
.
~blocking
controls the internal strategy Lwt uses to perform I/O on the underlying fd
. Regardless of ~blocking
, at the API level, Lwt_unix.read
, Lwt_unix.write
, etc. on fd'
always block the Lwt promise, but never block the whole process. However, for performance reasons, it is important that ~blocking
match the actual blocking mode of fd
.
If ~blocking
is not specified, of_unix_file_descr
chooses non-blocking mode for Unix sockets, Unix pipes, and Windows sockets, and blocking mode for everything else. Note: not specifying ~blocking
causes fstat
to be lazily called on fd
, the first time your code performs I/O on fd'
. This fstat
call can be expensive, so if you use of_unix_file_descr
a lot, be sure to specify ~blocking
explicitly.
of_unix_file_descr
runs a system call to set the specified or chosen blocking mode on the underlying fd
.
To prevent of_unix_file_descr
from running this system call, you can pass ~set_flags:false
. Note that, in this case, if ~blocking
, whether passed explicitly or chosen by Lwt, does not match the true blocking mode of the underlying fd
, I/O on fd'
will suffer performance degradation.
Note that ~set_flags
is effectively always false
if running on Windows and fd
is not a socket.
Generally, non-blocking I/O is faster: for blocking I/O, Lwt typically has to run system calls in worker threads to avoid blocking the process. See your system documentation for whether particular kinds of file descriptors support non-blocking I/O.