
Exploring Lua Coroutines in ComputerCraft - ColinWright
http://spin.atomicobject.com/2013/04/26/lua-coroutines-computercraft/
======
iamwil
I'm not exactly clear on how he's using coroutines to handle async callbacks.
Can anyone help explain it?

~~~
chrisfarber
Sure. I basically take the async callback and wrap it in a coroutine before I
invoke it. If the coroutine yields back a function, I assume that that
function can be repeatedly invoked until it returns a truthy value indicating
it wants to resume. Unfinished coroutines are then kept in a table, and after
each event from the ComputerCraft environment, I check the table of unfinished
callbacks to see which ones can be resumed.

------
agentultra
We do this a lot in Python (use co-routines and generators to break out of
callback chains and make async code look synchronous). See Twisted, Tulip,
etc.

Cool article... didn't know about ComputerCraft. Looks neat. :)

~~~
silentbicycle
In Python terms, Lua is "stackless", in the same sense as Stackless Python
(<http://www.stackless.com/>).

------
stcredzero
Has someone implemented the same subset of CSP as in Go, but done it in Lua?
Could it be done? Does concurrency in Lua depend on everyone calling yield
when they're supposed to?

~~~
silentbicycle
Concurrency in Lua's core language is co-operative (coroutines explicitly
yield to each other). This is because the language designers don't want to
impose any particular threading architecture -- Lua is intentded to be as
portable and non-opinionated about that sort of thing as possible. Its
coroutines are intended for the same sort of issues as
generators/callbacks/continuations, not for explicit parallelism. They won't
do multi-core mapreduce, but they'll let you cleanly factor code that has
multiple main loops.

You can get multi-threaded/multi-process concurrency by messaging between
separate Lua VMs, using libraries such as Lua Lanes
(<http://kotisivu.dnainternet.net/askok/bin/lanes/>). The Lua VM is
implemented without static state, so several VMs can run in the same process
without a GIL.

There is LuaPi (<https://github.com/LuaDist/luapi>), an implementation of the
Pi calculus (a variant of CSP), but I don't really know much about it. I'm
more familiar with Erlang-flavored concurrency models (and there's also
ConcurrentLua, <https://github.com/lefcha/concurrentlua>).

~~~
catwell
In addition to that you may want to look at <https://github.com/loyso/LuaCSP>
\- it is not implemented in Lua but it extends the language with CSP.

~~~
stcredzero
I wonder why this guy says FSM are non-composable? I've done something that
could be termed as "orthogonal" combination of multiple FSMs. It's also
possible for one FSM to "freeze" the operation of another and "unfreeze" a
3rd. I know, because I've done it.

------
alttab
Interesting. I havent used co-routines before.

