Coroutine Status
Check coroutine state with coroutine.status: running, suspended, dead.
Four Coroutine States
Coroutines have four possible states: suspended (created or yielded, waiting to run), running (currently executing), normal (paused to let another coroutine run), and dead (finished or errored). Query with coroutine.status(co).
local co = coroutine.create(function()
print(coroutine.status(coroutine.running())) -- running
coroutine.yield()
end)
print(coroutine.status(co)) -- suspended
coroutine.resume(co) -- prints: running
print(coroutine.status(co)) -- suspended
coroutine.resume(co)
print(coroutine.status(co)) -- deadThe "normal" State
A coroutine enters the "normal" state when it resumes another coroutine. It's not running (the other coroutine is), but it's not suspended either — it's waiting for the resumed coroutine to yield. This state is rare but important to understand for complex coroutine graphs.
local inner, outer
inner = coroutine.create(function()
print("outer status:", coroutine.status(outer)) -- normal
coroutine.yield()
end)
outer = coroutine.create(function()
coroutine.resume(inner)
end)
coroutine.resume(outer)
-- prints: outer status: normalAll lessons in this course
- Creating Coroutines
- resume and yield
- Coroutine Status
- Producer-Consumer Pattern