Lumen: Lua Multitasking Environment.
“A nice generic framework to develop complex, portable concurrent applications in Lua.”
How does it look?
Here is a small program, with two tasks: one emits ten numbered signals, one second apart. Another tasks receives those signals and prints them.
local sched=require 'lumen.sched'
-- task receives signals
sched.run(function()
local waitd = {'an_event'}
while true do
local _, data = sched.wait(waitd)
print(data)
end
end)
-- task emits signals
sched.run(function()
for i = 1, 10 do
sched.signal('an_event', i)
sched.sleep(1)
end
end)
sched.loop()
Tasks
Tasks can emit signals, and block waiting for them, and that’s it.
- A signal can be of any type, and carry any parameters
- A task can wait on several signals, with an optional timeout.
- Signals can be buffered; this helps avoid losing signals when waiting signals in a loop.
- There is an catalog that can be used to simplify sharing data between tasks.
Mutexes
There are cases when you must guarantee that only one task is accessing a piece of code at a time. Mutexes provide a mechanism for that. Notice that Lumen, being a cooperative scheduler, will never preempt control from a task. That means you only may have to resort to mutexes when your critical piece of code relinquish control explicitly, for example with a call to sleep, emitting a signal or blocking waiting for a signal.
Goodies
There are a few other useful modules, like an integrated remote Lua shell and a lightweigth HTTP server.
How to try it out?
There several test programs in the tests/ folder. This example has a few tasks exchanging messages, showing off basic functionality:
lua test.lua
Who?
Copyright © 2012 Jorge Visca, jvisca@fing.edu.uy
Contributors
Andrew Starks (@andrewstarks)