by majek

majek /lua-channels

Go style channels in pure Lua

131 Stars 7 Forks Last release: Not found Other 19 Commits 1 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Build Status


Go style Channels for Lua

This code is derived from libtask library by Russ Cox, mainly from channel.c. Semantically channels as implemented here are quite similar to channels from the Go language.


This is an example of using an unbuffered channel:

local task = require('lua-channels')

local function counter(channel) local i = 1 while true do channel:send(i) i = i + 1 end end

local function main() local channel = task.Channel:new() task.spawn(counter, channel) assert(channel:recv() == 1) assert(channel:recv() == 2) assert(channel:recv() == 3) end

task.spawn(main) task.scheduler()

lua-channels exposes:

  • task.spawn(fun, [...]) - run fun as a coroutine with given parameters. You should use this instead of coroutine.create()

  • task.scheduler() - can be run only from the main thread, executes all the stuff, resumes the coroutines that are blocked on channels that became available. You can only do non-blocking sends / receives from the main thread.

  • task.Channel:new([buffer size]) - create a new channel with given size

  • task.chanalt(alts, can_block) - run alt / select / multiplex over the alts structure. For example:

  • task.chanalt({{c = channel1, op = task.RECV}, {c = channel2, op = task.SEND, p = "hello"}}, true)

This will block current coroutine until it's possible to receive from channel1 or send to channel2. chanalt returns a number of statement from alts that succeeded (1 or 2 here) and a received value if executed statement was RECV.

Finally, if two alt statements can be fulfilled at the same time, we use math.random() to decide which one should go first. So it makes sense to initialize seed with something random. If you don't have access to an entropy source you can do:


but beware, the results of random() will predictable to a attacker.


You may simply require src/lua-channels.lua from the source, or install

from luarocks.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.