lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Jan 26, 2006 at 04:19:43PM -0500, Jon Smirl wrote:
> On 1/26/06, Glenn Maynard <glenn@zewt.org> wrote:
> > FWIW, I don't think this works with disk files on traditional Linux kernels;
right, but see notes on AIO e.g. in http://www.kegel.com/c10k.html

> > select() always returns them, and I/O always blocks.  Blocking calls to
> > read a file typically wait more briefly, of course, but they do block,
> > resulting in unused time if no other threads are running.  If you're reading
> > from, say, a CD-ROM, seeks may cause much longer blocking.
> 
> You want to open the files with O_NONBLOCK and assume that they will
> block in the lua code. Local file systems may ignore O_NONBLOCK and
> briefly block you in the kernel. But what if the file is on a network
> device? In that case O_NONBLOCK will be implemented since it may take
> a while for the file IO to complete.
no, it is not implemented.
It's not about speed, but about seekable (random access) files vs. streams.

A stream has a buffer, on the reaading end you can do nothing but wait
for the other end to fill it, but once there is input it will sit there
immediatly available until you read it.
For a seekable file, on the other hand, input is immediatly available
if it's in the cache, which is filled by the read call or by other
processes reading the same or by kernel read ahead or whatever,
and likewise the input may vanish from the cache at any time.

That's why select does not work with seekable files,
and hence neither does O_NONBLOCK
(as it's quite useless without a means to detect a state change).

> > implement read()=EAGAIN, call out to a registered callback if present,
> > epoll_wait(), get event, return.
> 
> Note that if the filesystem doesn't implement O_NONBLOCK you simply
> won't get the EAGAIN return from the read call. If you code assuming
> O_NONBLOCK is implemented the same code will work if it is not.
> 
> lua should definitely provide a run-time implementation of sendfile()
> which uses the underlying OS implementation (they all have it).
> sendfile() is an example of something that definitely provides the
> EAGAIN, non-blocking return.
> 
> Think about the problem of a 56K dialup modem asking for a 2MB file
> from a lua web server. It is going to to take eight minutes to send
> the file to the modem. Obviously async IO has to be used or the web
> server will be useless.
No, async IO (like aio or the win overlapping style) is very rarely used
in webservers, and many do not even use nonblocking IO but rely on a
thread or process pool to just do the waiting.

> If you set both the net and disk fd to
> O_NONBLOCK and then call sendfile() you will get a stream of
> EAGAIN/epoll events that will allow you to multitask. Apache,
> lighttpd, etc all do this.
It is nonblocking on the net end only.
That's why an aio_sendfile() call is on many whishlists.


regards