for a more detailed discussion
There are 2 fundamental design choices here
- are the callbacks to share a common Lua universe ?
- is the main processing loop in Lua or c ?
The simplest (and fastest ?) model here would be to have the main loop in
c(++), using os threading and events to run each lua callback in it's own
lua state (vm). We found this model perfect for a specific large commercial
nb. the ability to clone a lua state would be lovely in this case, as we
could load all libraries and perform initialisation of one "master" state,
which is then cloned for each child thread.
If we want the main loop to be in Lua, we need an efficient way for Lua to
sleep between events. We can use select() for socket io, but other events,
signals, etc become os-specific.
A single shared vm also increases granularity (response time) : events can
only be serviced between lua instructions. This will suffice for most
If we want the event handlers to be interruptible, we need to check for
events between lua instructions, and yield to the scheduler as required.
This can be simulated using the debug linehook, but that imposes a fair
amount of overhead - the event test should be in the core lua vm.
The callback handlers otherwise need to be written with multiplexing in
mind, and yield regularly to the scheduler.
4. Multiplexed socket/file io.
- See #2 re file io.
A simple way to handle socket multiplexing is to have a main scheduler based
around select(), and to override the read() and write() functions to yield
back to the scheduler if they would block. This allows re-use of existing
blocking socket code unchanged.
I have built a library which emulates/wraps the luasocket interface, using
this approach; i can thus re-use Diego's http libraries (thanks Diego). This
lib was first developed for a http proxy prototype, and is fine for
light-duty web serving / prototyping. I have tested it with 50 simultaneous
http requests on windows without a problem : it runs into winsock issues
when i try too many requests.
I found it a very nice model for simple multiplexing :
-- sample code ----------------------------
-- a, b are connected sockets
-- this fn is called from a couroutine
while true do
local s, err = a:read("*l") -- yields if insufficient data
if err then return nil, err
if not s then return true end
local ret, err = b:write(s.."\n") -- yields until done
if err then return nil, err
end -- sample ------------------------------
The main drawback with this approach is response time : the scheduler is
only invoked when a coro explicitly yields, or calls one of the yielding io
It is also difficult to fully exploit native async. io (IOCP in windows).
ps. A wonderful side-effect of using coroutines is the error behaviour - you
effectively get a free "try", and errors can be propgated / handled in a
very flexible manner.
pps For high performance web serving, Apache + FastCGI Lua ?
ppps These comments represent my limited problem set; actual mileage may vary.
On Tuesday 31 January 2006 1:12 am, Adrian Sietsma wrote:
> The simplest (and fastest ?) model here would be to have the main loop
> in c(++), using os threading and events to run each lua callback in it's
> own lua state (vm). We found this model perfect for a specific large
> commercial task.
since there's been a lot of previous work on this subject in c(++); it's
relatively easy for a specific project to choose one model and go with it.
but to make it usefull for everybody, the Lua way would be to get a nice set
of primitives and let the user build his own dispatcher.
i'm trying to write something (currently in the third rewrite since the thread
started) that would provide a single Lua interface to build the dispatcher,
and at the same time, a simple set of C routines to build IO libraries.
at the moment, my model is a bit like this:
the library writer splits each potentially blocking operation in three C
int (*begin)(lua_State *L, void **userdata);
void (*work)(void *userdata);
void (*end)(lua_State *L, void *userdata);
when the Lua developer calls the function, only the begin C function is
called; it sets up it's userdata, and the framework returns a "future" to the
all futures are contained in a queue, and a set of helper threads call the
work functions asynchronously. this work functions can't modify the Lua
State. when finished, the framework puts the future in the 'done' queue.
the dispatcher (written in Lua) calls a function that returns the done futures
from this queue (might block if there's nothing there).
with the 'done' future, the Lua code calls a 'finish' method, the end function
is executed, and any data from the operation is returned.
this is (i think) enough to write a simple dispatcher: just wrap the new
nonblocking operations so that they yield out of the coroutine 'thread' with
the future; the dispatcher associates the unfinished future with the thread,
and resumes it when this same future comes out of the done queue. the
wrapper calls the finish method and returns the result to the original
i'm still mangled in code; mostly about who gets the responsibility to dealloc
the futures, and if they should be reused for sequential operations.