[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Flushing global variables
- From: Romulo <romuloab@...>
- Date: Thu, 19 May 2011 11:48:29 -0300
On Wed, May 18, 2011 at 20:34, Murray S. Kucherawy <msk@cloudmark.com> wrote:
> I’m working on an application that’s multi-threaded, and to avoid a
> newstate-load-pcall-close series for every time I want to invoke a script,
> I’m thinking of allocating a pool of states and just pulling one out when
> it’s time to run, which leaves me with just load-pcall every time.
If I understand what you are asking, then you may setfenv the loaded function:
local env = setmetatable( {}, { __index = _G } )
setfenv( loadedscript, env )
local ok, err = pcall( loadedscript, param1, param2, param3 )
setfenv( loadedscript, {} ) -- remove reference to "env"
env = nil
collectgarbage( 'collect' )
Where "loadedscript" is the function to be executed at each request.
With this approach, every global variable set by "loadedscript" will
be discarded at the next gc cycle.
However, if there are any modules that use the original global table,
keep values on locals, or keep values on the registry, those values
won't be collected. Globals that hold subtables and tables on closures
won't be collected either.
Conclusion: if you want complete isolation and don't have control over
the globals/locals/closure /registry usage of all modules used by the
script, then you shall use one state for each request. Otherwise, the
setfenv method will work fine.
--rb