Hacker News new | comments | show | ask | jobs | submit login
Exploring Lua Coroutines in ComputerCraft (atomicobject.com)
27 points by ColinWright 1635 days ago | hide | past | web | 10 comments | favorite



I'm not exactly clear on how he's using coroutines to handle async callbacks. Can anyone help explain it?


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.


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. :)


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


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?


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).


There's some other choices which are interesting to me:

luaproc as described in http://www.inf.puc-rio.br/~roberto/docs/ry08-05.pdf is nice and simple. I'm also interested in lua-zmq.threads for LuaJIT which uses zeromq for message passing between threads. http://lua-users.org/lists/lua-l/2011-04/msg00364.html and https://github.com/Neopallium/lua-zmq


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.


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.


Interesting. I havent used co-routines before.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: