4 ms·
Exploring Lua Coroutines in ComputerCraft
- alttab 14y agoInteresting. I havent used co-routines before.
- stcredzero 14y agoHas 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 14y agoConcurrency 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/ 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 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 https://github.com/lefcha/concurrentlua).
- catwell 14y agoIn addition to that you may want to look at https://github.com/loyso/LuaCSP https://github.com/loyso/LuaCSP - it is not implemented in Lua but it extends the language with CSP.
- stcredzero 14y agoI 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.
- asb 14y agoThere's some other choices which are interesting to me: luaproc as described in http://www.inf.puc-rio.br/~roberto/docs/ry08-05.pdf 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 http://lua-users.org/lists/lua-l/2011-04/msg00364.html and https://github.com/Neopallium/lua-zmq https://github.com/Neopallium/lua-zmq
- iamwil 14y agoI'm not exactly clear on how he's using coroutines to handle async callbacks. Can anyone help explain it?
- chrisfarber 14y agoSure. 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 14y agoWe 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 14y agoIn Python terms, Lua is "stackless", in the same sense as Stackless Python (http://www.stackless.com/ http://www.stackless.com/).