2013-05-08 102 views
1

我編寫了一些調用Lua的C語言。有三個Lua文件:init.luaredis_pool.luarun.lua。首先,我初始化Redis的池redis_pool.lua(主叫init.luainit.lua調用redis_pool.lua),以及redis_pool.lua好像是:如何存儲在Lua中初始化的數據?

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

    -- redis_pool.lua 
    local redis_pool = {} 

    function init_pools() 
      -- init data in redis_pool 
    end 

    function redis_pool.get_pool(pool_name) 
      -- return one of redis in @redis_pool 
      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 = {...}} 

      -- some other functions... 
    } 

現在,我想表redis_pool準備好,然後我CAL凌run.lua用C

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

    function run_func 
      -- error, redis_pool['pool1'] is nil!! 
      local pool = redis_pool.get_pool('pool1') 
    end 

我已經初始化表redis_pool,但爲什麼它成爲nil而C調用另一個Lua中訪問它? 我是否必須將redis_pool返回到C堆棧,並將它傳遞給連續的Lua訪問函數?


更新

其中的一些C代碼:

/* C code to call init.lua */ 
    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; 
      } 

      /* preload */ 
      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; 
    } 

    /* calling run.lua from C */ 
    int some_func() { 
      ... 
      ret = luaL_loadfile(ls, "run.lua"); 

      ... 
      lua_getglobal(ls, "run_func") 
      ret = lua_pcall(ls, 0, 0, 0) 
      if (ret) { 
        /* error here */ 
        err = lua_tostring(ls, -1); 
        return -1; 
      } 

      ... 
      return 0; 
    } 
+0

請發佈運行文件的C代碼部分。 – 2013-05-08 11:31:23

+0

@llmo Euro:更新了C代碼。 – coanor 2013-05-08 11:49:01

回答

3

你有兩個獨立的Lua初始化和使用狀態:

/* C code to call init.lua */ 
int init_redis_pool(void) { 
     int ret = 0; 
     lua_State *ls = luaL_newstate(); // ls is a local variable 
     luaL_openlibs(ls); 
     ret = luaL_loadfile(ls, "init.lua"); 


/* calling run.lua from C */ 
int some_func() { 
     ... 
     ret = luaL_loadfile(ls, "run.lua"); // ls is another local variable 

當加載init.lua和初始化池,更改只有一個pply到你的本地ls變量。當您在另一個功能中訪問run.lua時,您之前的Lua狀態已經關閉並被銷燬。

你需要在功能之間分享你的Lua狀態變量。一種方法是在兩個函數之外創建狀態並將其傳遞給每個函數:

/* C code to call init.lua */ 
int init_redis_pool(lua_State *ls) { 

/* calling run.lua from C */ 
int some_func(lua_State *ls) { 
     ... 
+0

如何從全局(傳入)'Lua_State'在另一個新的'Lua_state'中獲取這些redis池? – coanor 2013-05-09 05:06:55

+0

@coanor對於你需要的其他狀態?你可以用C存儲對象,然後將它們傳遞給另一個Lua狀態,但我不明白爲什麼。 – 2013-05-09 05:48:07

+0

@llmo Euro http://stackoverflow.com/questions/16453654/how-to-pass-data-between-multiple-lua-statemulti-thread – coanor 2013-05-09 06:09:02