如何存储在Lua中初始化的数据?

我编写了一些调用Lua的C代码,其中有三个Lua文件:_init.lua_、_redis_pool.lua_和_run.lua_。首先,在_redis_pool.lua_中初始化了redis池(调用_init.lua_,而_init.lua_调用_redis_pool.lua_),_redis_pool.lua_看起来像这样:

    -- init.lua
    local redis_pool = require('redis_pool')
    redis_pool.init_pools()
    ...

    -- redis_pool.lua
    local redis_pool = {}

    function init_pools()
            -- 在redis池中初始化数据
    end

    function redis_pool.get_pool(pool_name)
            -- 返回@redis_pool中的一个redis
            return redis_pool[pool_name]
    end

初始化后,表redis_pool看起来像这样:

    redis_pool = {
            ['pool1'] = {pool_sz, pool = {...}}
            ['pool2'] = {pool_sz, pool = {...}}
            ['pool3'] = {pool_sz, pool = {...}}
            ['pool4'] = {pool_sz, pool = {...}}

            -- 一些其他函数...
    }

现在,我认为表redis_pool已经准备好了,那么我在C中调用_run.lua_:

    -- run.lua
    local redis_pool = require('redis_pool')

    function run_func
            -- 错误,redis_pool['pool1']为nil!!
            local pool = redis_pool.get_pool('pool1')
    end

我已经初始化了表redis_pool,但为什么在C调用另一个Lua来访问它时它变成了nil?我需要把redis_pool返回到C堆栈,并传递给后续的Lua访问函数吗?


更新

这是一些C代码:

    /* 调用init.lua的C代码 */
    int init_redis_pool(void) {
            int ret = 0;
            lua_State *ls = luaL_newstate();
            luaL_openlibs(ls);
            ret = luaL_loadfile(ls, "init.lua");
            const char *err;
            (void)err;

            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            /* 预加载 */
            ret = lua_pcall(ls, 0, 0, 0);
            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            lua_getglobal(ls, "init_pools");
            ret = lua_pcall(ls, 0, 0, 0);
            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1
            }

            lua_close(ls);

            return 0;
    }

    /* 从C中调用run.lua */
    int some_func() {
            ...
            ret = luaL_loadfile(ls, "run.lua");

            ...
            lua_getglobal(ls, "run_func")
            ret = lua_pcall(ls, 0, 0, 0)
            if (ret) {
                    /* 错误在这里 */
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            ...
            return 0;
    }
点赞
用户1283954
用户1283954

你需要两个独立的 Lua 状态来进行初始化和使用:

/* 调用 init.lua  的 C 代码 */
int init_redis_pool(void) {
        int ret = 0;
        lua_State *ls = luaL_newstate(); // ls是一个局部变量
        luaL_openlibs(ls);
        ret = luaL_loadfile(ls, "init.lua");

/* 从 C 中调用 run.lua */
int some_func() {
        ...
        ret = luaL_loadfile(ls, "run.lua"); // ls 是另一个局部变量

当你加载 init.lua 和初始化 pool 的时候,更改只适用于你的 本地 ls 变量。当你在另一个函数中访问 run.lua 时,你之前的 Lua 状态已经被关闭和销毁。

你需要在两个函数之间共享你的 Lua 状态变量。一种方式是在两个函数之外创建状态并将其传递给每个函数:

/* 调用 init.lua 的 C 代码 */
int init_redis_pool(lua_State *ls) {

/* 从 C 中调用 run.lua */
int some_func(lua_State *ls) {
        ...
2013-05-08 11:56:49