首页 > 解决方案 > Lua:为什么这个协程循环没有完成?

问题描述


我正在编写用于自动测试舞台灯光控制台(GrandMA2)插件的脚本。由于插件需要一些用户输入,因此测试主要是输入模糊测试,因此插件会运行多次。
这是通过使用运行测试脚本的协程来完成的,并且每次运行主插件都会产生收益。主插件完成后,它将恢复测试线程。重复执行此操作,直到测试文件中硬编码的所有输入值都经过测试。这就是理论。在实践中,会出现插件有时只运行一次的问题。如果我关闭控制台软件,重新打开它(不保存文件),导入插件并再次以测试模式启动它,它会运行 2 或 3 次。重复这个,它现在运行 4-5 次,依此类推。但它永远不会经常运行以测试所有值。最好的情况是它在插件最后一次运行之前停止。
你可以看看下面的一些代码。但如果你受到鼓励,你也可以在 GitHub (https://github.com/kochjo/CueStep-Fader) 上阅读整个代码。你只需要注意文件 ```CSF_main.lua``` 和 ```Test.lua```。
这对插件本身来说并不重要,但由于我仍在学习编码,我想知道为什么会这样。
......这让我发疯了。
**主文件中的代码片段:**
local REQ_SUCCESS, Test = pcall(function() return require 'Test' end)
-- do some other stuff..
function CSF_main(testmode)
    --[[
        Requests user input for the number of steps and the CSF-name
        and executes all necessary functions in order.
    ]]
    if testmode and REQ_SUCCESS then
        TESTMODE = true
    elseif not REQ_SUCCESS then
        gma.echo('CSF plugin: Test script not found. Thus test mode is not available.')
    end
    -- do some other uninteresting stuff..
    if TESTMODE then coroutine.resume(Test.test); gma.echo("resumed") end
end
return CSF_main

测试文件中的代码片段:

Test.test = coroutine.create(function()
    --[[
        The main function for the test. It runs CSF repeatedly (with param. 'testmode' set to true)
        until all inputs listed in Test.lua are tested.
    ]]
    local run = 1
    while not all_tested() do
        log("Run", run, 'Not all tested. Next one.')
        cmd('LUA "CSF_main(true)"') -- execute the main plugin again via the console API..
        coroutine.yield() -- wait for the current run of CSF to finish.
        log(nil, nil, "\n Resume yielding loop.")
        run = run+1
    end
    log(nil, nil, "Test completed.") -- this is never reached..
    LOGFILE:close()
end)

这是我在 stackoverflow 上的第一个问题,我希望我能以某种方式解释所有内容,让不了解 GrandMA2 照明台及其 lua API 的人也能理解我的问题所在。提前感谢为此付出时间的每一个人!

标签: lua

解决方案


推荐阅读