There are multiple ways to achieve your goal.
Within your example both fibers (main
and time_consuming
task) share the same scope - status
variable. If you change the value of the status
variable in the child fiber, parent fiber will see an update. You can check this using simple snippet:
status = 1
fiber.create(function() status = 0 end)
print(status) -- 0
Now, to catch an exception, use pcall
function. It accepts a function as a first argument, calls it and returns status as a first value following one or more function results. There is also xpcall
function if you want to analyse the error being caught. It takes error handler as a second argument.
With pcall
you may change your time_consuming
function like this:
local function time_consuming()
local ok = pcall(function() lua_error end)
if not ok then
status = 0
end
end
and status
will successfully updated if lua_error
fails.
But usually I consider this as a bad practice. If more fibers share the same state, it may become difficult to maintain due to uncertainty of the order of fiber execution. So for more reliable solution you may want to use interfiber communication primitives aka channels. You will be able to explicitly ask child fiber to tell you its execution status whether it succeed or not using channel:put()
and channel:get()
function. See documentation for examples.