【问题标题】:Problem with hot reloading Lua app on TarantoolTarantool 上热重载 Lua 应用程序的问题
【发布时间】:2020-09-22 03:41:57
【问题描述】:

我正在尝试热重载 Lua 模块,但执行此操作的标准方法似乎不适用于我的情况。

我创建了 2 个简单的示例模块,“app.lua”和“test.lua”,前者用作应用程序的入口点:

# app.lua
test2 = require("test")

while 1 > 0 do
    test2.p()
end

并从后者加载一个函数:

# test.lua
local test = {}
function test.p()
    print("!!!")
end

return test

此应用程序在由官方 Tarantool 映像构建的 docker 容器中运行。假设我对“测试”模块的代码进行了更改,例如,将带有打印的行更改为“打印(“???”)'。重新加载模块的标准方法是进入容器上的 tarantool 控制台并将nil 分配给package.loaded['<name_module>']。但是,当我输入它时,控制台说它已经为空:

tarantool> package.loaded['test']
---
- null
...

我在这里做错了什么?

【问题讨论】:

    标签: lua tarantool


    【解决方案1】:

    您可能会看到package.loaded['test'] == nil,因为您没有连接到 Tarantool 实例。

    通常当你连接到 Tarantool 时,你看起来像

    connected to localhost:3301
    localhost:3301> 
    

    似乎您只需进入 docker 容器,然后 运行“tarantool”。这样,您只需运行对您的应用程序一无所知的新 Tarantool 实例。

    您可以使用console 命令(在容器中)或tarantoolctl connect login:password@host:port(默认配置tarantoolctl connect 3301 有效,详细信息请参阅here)或attach 连接到tarantool 实例,然后检查package.loaded['test'] 值.

    这是重新加载模块代码的简化方法:

    test2 = require("test")
    
    local function reload()
        package.loaded['test'] = nil -- clean module cache
        test2 = require('test') -- update a reference to test2 with new code
    end
    
    while 1 > 0 do
        test2.p()
    end
    
    return {
       reload = reload,  -- require('app').reload() in your console for reload
    }
    

    更复杂但正确的方法是使用package-reload 模块。

    以下是您的代码不起作用的解释:

    -- Here you require "test" module
    -- Lua check package.loaded['test'] value
    -- and if it's nil then physically load file
    -- from disk (see dofile function).
    --
    -- Well, you got a reference to table with
    -- your "p" function.
    test2 = require("test")
    
    -- Here you've already has a reference
    -- to "test" module.
    -- It's static, you don't touch it here.
    while 1 > 0 do
        test2.p()
    end
    

    然后你做package.loaded['test'] = nil 和 从package.loaded 表中删除一个键。 请注意,您不会因为拥有 “app.lua”文件中的引用(在您的情况下为 test2)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 2017-05-13
      • 2019-09-14
      • 2022-07-18
      • 1970-01-01
      • 2021-09-03
      • 2019-04-14
      相关资源
      最近更新 更多