coroutine
A coroutine is used to perform multiple tasks at the same time from within the same script. Such tasks might include producing values from inputs or performing work on a subroutine when solving a larger problem. A task doesn’t even need to have a defined ending point, but it does need to define particular times at which it will yield (pause) to let other things be worked on.
Using Coroutines
A new coroutine can be created by providing a function to coroutine.create()
. Once created, a coroutine doesn’t begin running until the first call to coroutine.resume()
which passes the arguments to the function. This call returns when the function either halts or calls coroutine.yield()
and, when this happens, coroutine.resume()
returns either the values returned by the function, the values sent to coroutine.yield()
, or an error message. If it does error, the second return value will instead be the thrown error.
During the lifetime of the coroutine, you can call coroutine.status()
to inspect its status:
Status | Meaning |
---|---|
suspended | The coroutine is waiting to be resumed. Coroutines begin in this state and enter it when their function calls |
running | The coroutine is running right now. |
normal | The coroutine is awaiting the yield of another coroutine; in other words, it has resumed another coroutine. |
dead | The function has halted (returned or thrown an error). The coroutine cannot be used further. |
Wrapping Coroutines
When working with coroutines, you can also forgo the use of the coroutine object and instead use a wrapper function. Such a wrapper function will resume a particular coroutine when it is called and will return only the yielded values. You can do this using coroutine.wrap()
:
The first value returned from coroutine.resume()
describes whether a coroutine ran without errors. However, functions returned by coroutine.wrap()
will not do this: instead they directly return the values returned or passed to coroutine.yield()
, if any. Should an error have occurred while running the coroutine function, the error is raised on the call of the returned function.
Producer Pattern Example
Imagine a task that produces repetitions of a word: each time it produces a repetition, the next one will produce one more. For example, providing Hello
will produce Hello
, HelloHello
, HelloHelloHello
, etc. To do this, you can define repeatThis()
:
To run this function as a coroutine, you can use coroutine.create()
followed by multiple calls to coroutine.resume()
:
For this producer function, you can also use coroutine.wrap()
to get a function that produces values:
Coroutine Functions
Creates a new coroutine, with body f. f must be a Lua function. |
Starts or continues the execution of coroutine |
thread coroutine.running ( ) |
Returns the running coroutine. |
Returns the status of coroutine co, as a string: ‘running’, if the coroutine is running (that is, it called status); ‘suspended’, if the coroutine is suspended in a call to yield, or if it has not started running yet; ‘normal’ if the coroutine is active but not running (that is, it has resumed another coroutine); and ‘dead’ if the coroutine has finished its body function, or if it has stopped with an error. |
Creates a new coroutine, with body f. f must be a Lua function. Returns a function that resumes the coroutine each time it is called. Any arguments passed to the function behave as the extra arguments to resume. Returns the same values returned by resume, except the first boolean. In case of error, propagates the error. |
Last updated