开启 lua_code_cache后偶尔出现的找不到全局函数错误

30 views
Skip to first unread message

Rezo Chiang

unread,
Mar 14, 2019, 12:05:17 AM3/14/19
to openresty
大家好, 现有如下代码:

local WRandom = {}
WRandom.__index = WRandom

function WeightedRandom()
 
local tab = {}
  tab
.items = {}
  setmetatable
(tab, WRandom)
 
return tab
end

function WRandom:clear()
   
for i,_ in ipairs(self.items) do
      table
.remove(self.items, i)
   
end
end

function WRandom:Add(weight, item)
 
local t = {}
  t
.weight = weight
  t
.item = item
  table
.insert(self.items, t)
end

return WRandom


另一个文件使用如下代码使用该文件
module(..., package.seeall)
local random  = require "WRandom"
local  s  = WeightedRandom()

程序运行一段时间后, 就会出现 attempt to call global 'WeightedRandom' (a nil value) 错误?

请问应该如何排查这样的问题呢?

Rezo Chiang

unread,
Mar 14, 2019, 4:47:59 AM3/14/19
to openresty
已定位问题: 有两个文件require并调用了该函数,  如果一个文件调用成功后, 另一个就会报错.
目前将WeightedRandom修改为 WRandom:Random并修改调用方式后. 问题解决.
但我还是没有搞清为何会如此,  知道的大神可否指点一二, 谢谢.

在 2019年3月14日星期四 UTC+8下午12:05:17,Rezo Chiang写道:
Reply all
Reply to author
Forward
0 new messages