lua-users home
lua-l archive

Advice on running and reusing multiple threaded Lua states

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


Dear colleagues,
I seek your advice on a system we (www.hitecnologia.com) have been building.
We are using Lua as the embedded scripting language for a supervisory system for
Industrial Automation Control. So far it was an excellent choice.
Since it was my first contact with Lua, I am uncertain about some design decisions. In the C++ app kernel, every time there is a demand for script execution, we create the Lua state, populate it with contextual info, load the standard Lua libs and assign
it to a (system-)thread retrieved from a pre-allocated thread pool.
The user code for the script is wrapped in a template "function <scriptName>(Param) <user code> end". When the script finishes execution, the thread is returned to the pool and the lua state
is destroyed. This is working, but I guess it can be improved.
My assumptions:
1) I did not use co-routines because the <user code> can do
 arbitrary blocking operations (DBs, file system, etc).
 Moreover, OS threads provide a simpler programming model
 for the script developer (sometimes they are Engineers with
 little background in programming).
 If I could detect the absence of blocking calls, perhaps I could
 transparently run the script in a coroutine instead of a OS thread.
 But I decided not even to try this, even though it sounds a nice
 research project.
My doubts:
1) Is it worth to pre-allocate a lua_State pool instead of creating it every time ? 2) If so, in between executions is it enough to clean it using settop(L,0) and
 replacing its _ENV for a empty table ?
3) Instead of wrapping <user code> in the template function, could I just push it
 as a Lua chunk and pcall()-it ?
4) I have a common set of wrapped C-callbacks that are registered in every Lua state. Could I prepare a prototypical Lua State and clone it in the new Lua state created
 with some performance gain instead of registering its contents ?
I thank you for your time.
Cheers
Rod Senra
http://rodrigo.senra.nom.br--
Atenciosamente
_Rodrigo Dias Arruda Senra_
*Engenheiro de Computação*//

AltStyle によって変換されたページ (->オリジナル) /