元 table 错误
Meta table error
我正在尝试设置一个用于在 torch 中穿线的玩具示例,但我从 运行 下面的代码中收到错误。我认为这可能只是我设置 table 的方式,但我不确定。
Threads = require 'threads'
Threads.serialization('threads.sharedserialize')
DataThreads = {}
DataThreads.__index = DataThreads
local result = {}
local unpack = unpack and unpack or table.unpack
function DataThreads.new(nThreads,opt_)
local self = {}
opt_ = opt_ or {}
self.threads = Threads(nThreads,
function()
print("background stuff")
end
)
self.threads:synchronize()
-- The below for loop is causing the error but the same :addjob() works later on
for i=1, nThreads do
self.threads:addjob(self._getFromThreads, self._pushResult)
end
return setmetatable(self,DataThreads)
end
function DataThreads._getFromThreads()
x,y = torch.uniform(),torch.uniform()
return x,y
end
function DataThreads._pushResult(...)
local res = {...}
if res == nil then
self.threads:synchronize()
end
result[1] = res
end
function DataThreads:getBatch()
self.threads:addjob(self._getFromThreads, self._pushResult)
self.threads:dojob()
local res = result[1]
result[1] = nil
if torch.type(res) == 'table' then
return unpack(res)
end
print(type(res))
return res
end
d = DataThreads.new(4)
错误发生在.new 函数的:addjob()
。但是稍后在 :getBatch()
函数中调用相同的 :addjob()
是可行的。在设置 metatable 之前,我是否不允许调用 ._getFromThreads(), ._getFromThreads()
函数?这是错误,我认为这意味着 ._getFromThreads()
没有返回任何东西;
/home/msmith/torch/install/bin/luajit: ...e/msmith/torch/install/share/lua/5.1/threads/threads.lua:215: function callback expected
stack traceback:
[C]: in function 'assert'
...e/msmith/torch/install/share/lua/5.1/threads/threads.lua:215: in function 'addjob'
threads.lua:21: in function 'new'
threads.lua:54: in main chunk
当您的 new
函数中的代码运行时,元表尚未设置,因此当您使用 self._getFromThreads
和 self._pushResult
时,那里什么也没有,您会得到 nil
回来,那是无效的。
它在 new
函数 returns 之后起作用,因为那时已经添加了元表,因此查找使用 __index
元方法查找条目。
你可以做到
local self = setmetatable({}, DataThreads)
立即设置元表,然后
return self
最后。
我正在尝试设置一个用于在 torch 中穿线的玩具示例,但我从 运行 下面的代码中收到错误。我认为这可能只是我设置 table 的方式,但我不确定。
Threads = require 'threads'
Threads.serialization('threads.sharedserialize')
DataThreads = {}
DataThreads.__index = DataThreads
local result = {}
local unpack = unpack and unpack or table.unpack
function DataThreads.new(nThreads,opt_)
local self = {}
opt_ = opt_ or {}
self.threads = Threads(nThreads,
function()
print("background stuff")
end
)
self.threads:synchronize()
-- The below for loop is causing the error but the same :addjob() works later on
for i=1, nThreads do
self.threads:addjob(self._getFromThreads, self._pushResult)
end
return setmetatable(self,DataThreads)
end
function DataThreads._getFromThreads()
x,y = torch.uniform(),torch.uniform()
return x,y
end
function DataThreads._pushResult(...)
local res = {...}
if res == nil then
self.threads:synchronize()
end
result[1] = res
end
function DataThreads:getBatch()
self.threads:addjob(self._getFromThreads, self._pushResult)
self.threads:dojob()
local res = result[1]
result[1] = nil
if torch.type(res) == 'table' then
return unpack(res)
end
print(type(res))
return res
end
d = DataThreads.new(4)
错误发生在.new 函数的:addjob()
。但是稍后在 :getBatch()
函数中调用相同的 :addjob()
是可行的。在设置 metatable 之前,我是否不允许调用 ._getFromThreads(), ._getFromThreads()
函数?这是错误,我认为这意味着 ._getFromThreads()
没有返回任何东西;
/home/msmith/torch/install/bin/luajit: ...e/msmith/torch/install/share/lua/5.1/threads/threads.lua:215: function callback expected
stack traceback:
[C]: in function 'assert'
...e/msmith/torch/install/share/lua/5.1/threads/threads.lua:215: in function 'addjob'
threads.lua:21: in function 'new'
threads.lua:54: in main chunk
当您的 new
函数中的代码运行时,元表尚未设置,因此当您使用 self._getFromThreads
和 self._pushResult
时,那里什么也没有,您会得到 nil
回来,那是无效的。
它在 new
函数 returns 之后起作用,因为那时已经添加了元表,因此查找使用 __index
元方法查找条目。
你可以做到
local self = setmetatable({}, DataThreads)
立即设置元表,然后
return self
最后。