我在中启动Redis连接池redis.lua,通过从 C 调用,我得到了redis_lua_state
,此 Lua 状态全局启动一次,仅在其他线程中启动get从中。
当有一个 HTTP 请求(工作线程)时,我需要从redis_lua_state
,然后new另一个Lua状态来加载其他Lua脚本,而这些脚本会使用这个redis连接来与Redis通信,怎么做呢?或者如何设计我的 Lua 脚本以使其变得简单?
代码示例:
/* on main thread, to init redis pool connection */
lua_State *g_ls = NULL;
lua_State *init_redis_pool(void) {
int ret = 0;
g_ls = luaL_newstate();
lua_State *ls = g_ls;
luaL_openlibs(ls);
ret = luaL_loadfile(ls, "redis.lua");
const char *err;
(void)err;
/* preload */
ret = lua_pcall(ls, 0, 0, 0);
lua_getglobal(ls, "init_redis_pool");
ret = lua_pcall(ls, 0, 0, 0);
return ls;
}
/* worker thread */
int worker() {
...
lua_State *ls = luaL_newstate();
ret = luaL_loadfile(ls, "run.lua");
/* How to fetch data from g_ls? */
...
lua_getglobal(ls, "run")
ret = lua_pcall(ls, 0, 0, 0)
lua_close(ls);
...
return 0;
}
如果你的 Lua 状态是分开的,那么就没有办法做到这一点。您的工作线程必须初始化 Redis 连接and对其进行处理。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)