Forcing a Lua script to exit

后端 未结 10 1014
长情又很酷
长情又很酷 2020-12-23 18:49

How do you end a long running Lua script?

I have two threads, one runs the main program and the other controls a user supplied Lua script. I need to kill the thread

10条回答
  •  离开以前
    2020-12-23 18:57

    I haven't found a way to cleanly kill a thread that is executing a long running lua script without relying on some intervention from the script itself. Here are some approaches I have taken in the past:

    1. If the script is long running it is most likely in some loop. The script can check the value of some global variable on each iteration. By setting this variable from outside of the script you can then terminate the thread.
    2. You can start the thread by using lua_resume. The script can then exit by using yield().
    3. You could provide your own implementation of pcall that checks for a specific type of error. The script could then call error() with a custom error type that your version of pcall could watch for:

      function()
          local there_is_an_error = do_something()
          if (there_is_an_error) then
              error({code = 900, msg = "Custom error"})
          end
      end
      

提交回复
热议问题