Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions examples/1-basic-usage/1-fibers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
--
-- Example ported from Go's Select https://go.dev/tour/concurrency/1

package.path = "../../src/?.lua;../?.lua;" .. package.path

package.path = "../../?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local sleep = require 'fibers.sleep'

local function say(string)
Expand All @@ -18,11 +17,7 @@ local function say(string)
end
end

fiber.spawn(function()
fiber.spawn(function() say("world") end)
fibers.run(function()
fibers.spawn(function() say("world") end)
say("hello")

fiber.stop()
end)

fiber.main()
11 changes: 5 additions & 6 deletions examples/1-basic-usage/10-luacat.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

-- Importing the necessary modules from the fibers framework
local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local file = require 'fibers.stream.file'
local socket = require 'fibers.stream.socket'
local sc = require 'fibers.utils.syscall'
Expand All @@ -21,7 +21,7 @@ end
local sock = socket.connect_unix(socketPath)

-- Fiber to read from stdin and write to the socket
fiber.spawn(function()
local function main()
while true do
local line = stdin:read('*l')
if line then
Expand All @@ -35,8 +35,7 @@ fiber.spawn(function()
-- Close the socket once done
stdin:close()
sock:close()
fiber.stop()
end)
end

-- Start the main fiber loop
fiber.main()
fibers.run(main)
16 changes: 7 additions & 9 deletions examples/1-basic-usage/2-channels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
--
-- Example ported from Go's Select https://go.dev/tour/concurrency/2

package.path = "../../src/?.lua;../?.lua;" .. package.path

package.path = "../../?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local channel = require 'fibers.channel'

local function sum(array, chan)
Expand All @@ -20,14 +19,13 @@ local function sum(array, chan)
chan:put(total)
end

fiber.spawn(function()
local function main()
local s = {7, 2, 8, -9, 4, 0}
local chan = channel.new()
fiber.spawn(function() sum({unpack(s,1,#s/2)}, chan) end)
fiber.spawn(function() sum({unpack(s,#s/2+1,#s)}, chan) end)
fibers.spawn(function() sum({unpack(s,1,#s/2)}, chan) end)
fibers.spawn(function() sum({unpack(s,#s/2+1,#s)}, chan) end)
local x, y = chan:get(), chan:get()
print(x, y, x+y)
fiber.stop()
end)
end

fiber.main()
fibers.run(main)
12 changes: 5 additions & 7 deletions examples/1-basic-usage/3-queues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
--
-- Example ported from Go's Select https://go.dev/tour/concurrency/3

package.path = "../../src/?.lua;../?.lua;" .. package.path

package.path = "../../?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local queue = require 'fibers.queue'

fiber.spawn(function()
local function main()
local q = queue.new(2)
q:put(1)
q:put(2)
print(q:get())
print(q:get())
fiber.stop()
end)
end

fiber.main()
fibers.run(main)
16 changes: 7 additions & 9 deletions examples/1-basic-usage/4-choices.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
-- Example ported from Go's Select https://go.dev/tour/concurrency/5


package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local channel = require 'fibers.channel'
local op = require 'fibers.op'

local perform, choice = op.perform, op.choice
local perform, choice = fibers.perform, fibers.choice

local function fibonacci(c, quit)
local x, y = 0, 1
Expand All @@ -31,17 +30,16 @@ local function fibonacci(c, quit)
until done
end

fiber.spawn(function()
local function main()
local c = channel.new()
local quit = channel.new()
fiber.spawn(function()
fibers.spawn(function()
for _=1, 10 do
print(c:get())
end
quit:put(0)
end)
fibonacci(c, quit)
fiber.stop()
end)
end

fiber.main()
fibers.run(main)
19 changes: 8 additions & 11 deletions examples/1-basic-usage/5-choices-alt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
--
-- Example ported from Go's Select https://go.dev/tour/concurrency/6

package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local channel = require 'fibers.channel'
local sleep = require 'fibers.sleep'
local op = require 'fibers.op'

local perform, choice = op.perform, op.choice
local perform, choice = fibers.perform, fibers.choice

-- time.After() is a Go library function
local function after(t)
local chan = channel.new()
fiber.spawn(function()
fibers.spawn(function()
sleep.sleep(t)
chan:put(1)
end)
Expand All @@ -26,7 +25,7 @@ end
-- time.Tick() is a Go library function
local function tick(t)
local chan = channel.new()
fiber.spawn(function()
fibers.spawn(function()
while true do
sleep.sleep(t)
chan:put(1)
Expand All @@ -35,7 +34,7 @@ local function tick(t)
return chan
end

fiber.spawn(function()
local function main()
local ticker = tick(0.1)
local boom = after(0.5)
local done = false
Expand All @@ -54,8 +53,6 @@ fiber.spawn(function()
end)
perform(task)
until done
end

fiber.stop()
end)

fiber.main()
fibers.run(main)
32 changes: 19 additions & 13 deletions examples/1-basic-usage/6-choices-adv.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

-- Importing the necessary modules
local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local sleep = require 'fibers.sleep'
local channel = require 'fibers.channel'
local queues = require 'fibers.queue'
local socket = require 'fibers.stream.socket'
local file = require 'fibers.stream.file'
local cond = require 'fibers.cond'
local op = require 'fibers.op'
local sc = require 'fibers.utils.syscall'

local perform, choice = op.perform, op.choice
local perform, choice = fibers.perform, fibers.choice

require("fibers.pollio").install_poll_io_handler()

Expand All @@ -21,37 +20,37 @@ local notif_chan = channel.new()
local exit_cond = cond.new()

-- Data Producer fiber
fiber.spawn(function()
local function producer()
while true do
-- sleep for some time to simulate work
sleep.sleep(math.random())
-- Send data to the queue
data_q:put(os.date('%Y-%m-%d %H:%M:%S'))
end
end)
end

-- Notifier fiber
fiber.spawn(function()
local function notifier()
while true do
-- sleep for some time to simulate work
sleep.sleep(4 * math.random())
-- Send data to the channel
notif_chan:put(1)
end
end)
end

-- Exit signaller
fiber.spawn(function()
local function exit()
while true do
-- sleep for some time to simulate work
sleep.sleep(30 * math.random())
-- Signal the condition
exit_cond:signal()
end
end)
end

-- Consumer fiber
fiber.spawn(function()
local function consumer()
-- file to write data to
local filename = "/tmp/data.txt"
os.execute("rm "..filename)
Expand Down Expand Up @@ -84,6 +83,13 @@ fiber.spawn(function()
)
perform(task)
end
end)
end

fiber.main()
local function main()
fibers.spawn(producer)
fibers.spawn(notifier)
fibers.spawn(exit)
consumer()
end

fibers.run(main)
12 changes: 5 additions & 7 deletions examples/1-basic-usage/7-exec.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
-- usage of the fibers.exec module

package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

local fiber = require 'fibers.fiber'
local fibers = require 'fibers'
local sleep = require 'fibers.sleep'
local exec = require 'fibers.exec'
local pollio = require 'fibers.pollio'

pollio.install_poll_io_handler()

local function main()
fiber.spawn(function() -- long running process where we want to periodically deal with output
fibers.spawn(function() -- long running process where we want to periodically deal with output
local cmd = exec.command('cat')
local stdin_pipe = assert(cmd:stdin_pipe())
local stdout_pipe = assert(cmd:stdout_pipe())
local err = cmd:start()
if err then error(err) end
fiber.spawn(function()
fibers.spawn(function()
for _ = 1, 4 do
stdin_pipe:write('tick')
sleep.sleep(0.2)
Expand All @@ -38,8 +38,6 @@ local function main()
local output, err = exec.command('sh', '-c', 'sleep 1; echo hello world; exit 255'):combined_output()
assert(err, "expected error!")
print("output:", output)
fiber.stop()
end

fiber.spawn(main)
fiber.main()
fibers.run(main)
21 changes: 11 additions & 10 deletions examples/1-basic-usage/8-ipc-simple.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- demonstrates IPC using exec and non-blocking sockets

package.path = "../../?.lua;../?.lua;" .. package.path
package.path = "../../src/?.lua;../?.lua;" .. package.path

local fiber = require "fibers.fiber"
local fibers = require "fibers"
local exec = require "fibers.exec"
local sleep = require "fibers.sleep"
local socket = require 'fibers.stream.socket'
Expand All @@ -15,7 +15,7 @@ local sockname = '/tmp/test-sock'
sc.unlink(sockname)
local server = assert(socket.listen_unix(sockname))

fiber.spawn(function()
local function receiver()
sleep.sleep(2) -- to show that things don't block and are gracefully buffered by sockets

while true do
Expand All @@ -29,10 +29,9 @@ fiber.spawn(function()
end
end
sc.unlink(sockname)
fiber.stop()
end)
end

fiber.spawn(function()
local function sender()
local messages = { "apple", "pear", "exit!" }
for _, v in ipairs(messages) do
print("sending:", v)
Expand All @@ -41,13 +40,15 @@ fiber.spawn(function()
local out, err = exec.command('sh', '-c', command):combined_output()
if err then error(out) end
end
end)
end

fiber.spawn(function()
local function main()
fibers.spawn(sender)
fibers.spawn(receiver)
while true do
print("hb")
sleep.sleep(1)
end
end)
end

fiber.main()
fibers.run(main)
Loading