lua-users home
lua-l archive

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

Daniel Colchete wrote:

> 1 - Scope pollution: and second time I run the script with the same
> lua_State* from the pool, the global variables set by the first run
> are still there. Is there a fast way to clean only the variables
> set by the Lua script?

> 2 - Global variables are not global anymore. One script doesn't
> have access to the global variables set by another instances. On
> thread doesn't talk to the other using lua entities: numbers,
> tables, etc...

My solution to this, inside of a webservice, was to have a one thread handle ALL the lua processing.  That thread listens to a (blocking) queue, waiting for a message, the message actually contains the script to be run.  It runs it and returns the results.  This may or may not solve problem #2, but if you keep the same environment active, you should be able to access the globals I believe.  

I use parts of MUSCLE ( for the message handling (flattened BeOS like messages, very handy), and pass them around in FIFOs.

Now the one problem with this approach is that if you need to process more than one script at a time it won't work very well.  A couple of possibilities come to mind (not particularly well thought out):

1.  Store off the global values you want to save between scripts with the C API, and restore them into the new environment each time you run a script.

2.  Have multiple LUA worker threads using the same environment, BUT you will have to wrap up the actual processing in a mutex or a critical section so you don't have them stepping on each other.

Good Luck!