Lua全局表与本地链接擦除

如何解决Lua全局表与本地链接擦除

我有一个由几个文件组成的模块,在其中声明了一个全局表,用于在函数之间传递消息。 据我所知,当我尝试清理全局表而不是main以外的文件时,发生冲突,这是由于我正在建立链接 local values = _G.values; >但是我这样做是为了提高访问此变量时的性能 如何清除另一个文件中的global(使用本地链接)表? 也许还有其他方法可以在模块文件之间传输共享数据,而无需使用对_G

的访问

模块:

main.lua
utils/a.lua
utils/b.lua

main.lua

os.setlocale("C")
package.path   = package.path .. ";./utils/?.lua";  -- luacheck: ignore
local Timer = require("timer")

_G.run = true;
_G.values = {};

local counter = 0;
local values = _G.values;
local timer_main = Timer:new()
timer_main:setInterval(1)

local timer_work = Timer:new()
timer_work:setInterval(10)


local a = require("utils.a");
local b = require("utils.b");

local function UpdateTable()
    counter = counter + 1
    print("Update table from main:       "..tostring(values))
    values[counter] = {Apple=counter+1,Banana=counter+2,Orange=counter+3}
end;


local function main()
    print("Global_main_start:            "..tostring(_G.values))
    print("Local_main_start:             "..tostring(values))
    while _G.run do
        if timer_main:isTimeout() then
            UpdateTable()
            print("Global_main_loop:             "..tostring(_G.values))
            print("Local_main_loop:              "..tostring(values))
            timer_main:setInterval(1)
        end
        b.TaskExecute()
        if timer_work:isTimeout() then
            _G.run = false
        end
    end
    print("Global_main_stop:             "..tostring(_G.values))
    print("Local_main_stop:              "..tostring(values))
end;


main()

a.lua

local Timer = require("timer")

local values = _G.values;
print("Local_after_require_a:        "..tostring(values))


local function EraseData()
    print("Global EraseData a before:    "..tostring(_G.values))
    print("Local EraseData a before:     "..tostring(values))
    _G.values = {}
    print("Global EraseData a after:     "..tostring(_G.values))
    print("Local EraseData a after:      "..tostring(values))
end;

return {
    EraseData = EraseData
}

b.lua

local Timer = require("timer")

local values = _G.values;
local a = a or require("a");
local timer_b = Timer:new()
timer_b:setInterval(3)
print("Local_after_require_b:        "..tostring(values))

local function TaskExecute()
    if timer_b:isTimeout() then
        print("Global Execute b before:      "..tostring(_G.values))
        print("Local Execute b before:       "..tostring(values))
        if #values > 0 then
            for i = 1,#values do
                print("         Values item: "..i.."       "..tostring(values[i]))
            end
        end
        a.EraseData()
        print("Global Execute b after:       "..tostring(_G.values))
        print("Local Execute b after:        "..tostring(values))
        timer_b:setInterval(3)
    end
end;

return {
    TaskExecute = TaskExecute
}

timer.lua(仅用于同时执行func)

local base   = _G;
local math   = math;
local os     = os;

local Timer = {}


local function new(self)
    local timer = {
        deadline = os.time()
    }
    base.setmetatable(timer,self)
    self.__index = self
    return timer
end

Timer.new = new

local function setInterval(self,duration)
    self.deadline = os.time() + duration
end

Timer.setInterval = setInterval

local function setDate(self,date)
    self.deadline = os.time(date)
end

Timer.setDate = setDate

local function getSecondsLeft(self)
    return math.max(0,self.deadline - os.time())
end

Timer.getSecondsLeft = getSecondsLeft


local function isTimeout(self)
    return os.time() >= self.deadline
end

Timer.isTimeout = isTimeout

return Timer

输出:

Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_b:        table: 0x7ff50ac09230
Global_main_start:            table: 0x7ff50ac09230
Local_main_start:             table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50ac09230
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
Global EraseData a before:    table: 0x7ff50ac09230
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50af05510   <--- Erased Global var in a.lua 
Local EraseData a after:      table: 0x7ff50ac09230   <--- Local var in a.lua not changed 
Global Execute b after:       table: 0x7ff50af05510   <--- Global var in b.lua changed 
Local Execute b after:        table: 0x7ff50ac09230   <--- Local var in b.lua not changed 
Update table from main:       table: 0x7ff50ac09230   <--- Add new values in old local var in main.lua
Global_main_loop:             table: 0x7ff50af05510  <--- Global var in main.lua changed 
Local_main_loop:              table: 0x7ff50ac09230  <--- Local var in main.lua not changed
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50af05510
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
Global EraseData a before:    table: 0x7ff50af05510
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004940
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004940
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50c004940
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
         Values item: 7       table: 0x7ff50ac09380
         Values item: 8       table: 0x7ff50ac09510
         Values item: 9       table: 0x7ff50c004350
Global EraseData a before:    table: 0x7ff50c004940
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004b90
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004b90
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004b90
Local_main_loop:              table: 0x7ff50ac09230
Global_main_stop:             table: 0x7ff50c004b90
Local_main_stop:              table: 0x7ff50ac09230

解决方法

执行UPDATE cust_item SET status = 'approved',lstupd_dtm = NOW() WHERE customer_id = '7301' AND item_id = '12498'; 不会“擦除”表。它仅将_G.values = {}的引用更改为新创建的表。上表_G.values指向的表完全不变,并且其他任何引用(例如_G.values的本地a)也将保持不变。您可以通过打印报表中显示的值来看到它。

values这是原始表格

table: 0x7ff50ac09230这是新表格

因为您没有更改table: 0x7ff50af05510,所以您不会看到table: 0x7ff50ac09230的本地a的任何更改。


我建议您将要清除的表值压低,使其位于您要引用的表中:

主要

values

a

messages = {}
messages.values = {}

我还发现没有必要在您提供的代码中使用local messages = _G.messages ... messages.values = {} ,如果您现在需要访问“原始”值,则只需将_G设置为正确的值就是全局值即可。由本地阴影遮盖,然后local values = values才有意义。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-