Rugo uses spawn to run code in the background. Each spawn block runs in
its own goroutine and returns a task handle you can use to get the result.
spawn
puts "working in background"
end
puts "main continues immediately"Capture the task and call .value to wait for the result:
use "http"
task = spawn
http.get("https://httpbin.org/get")
end
# .value blocks until the task finishes
body = task.value
puts bodyFor single expressions, skip the block:
task = spawn http.get("https://httpbin.org/get")
puts task.valueSpawn multiple tasks and collect results:
use "http"
urls = ["https://httpbin.org/get", "https://httpbin.org/ip"]
tasks = []
for url in urls
t = spawn http.get(url)
tasks = append(tasks, t)
end
for t in tasks
puts t.value
endIf a spawn block panics, the error is captured. Calling .value re-raises
it — compose with try/or for safe handling:
task = spawn
http.get("https://doesnotexist.invalid")
end
body = try task.value or "request failed"
puts bodyUse .done to check if a task has finished without blocking:
task = spawn `sleep 2 && echo done`
while !task.done
puts "still waiting..."
`sleep 1`
end
puts task.valueRun multiple expressions concurrently and wait for all results:
use "http"
results = parallel
http.get("https://api.example.com/users")
http.get("https://api.example.com/posts")
end
puts results[0] # users response
puts results[1] # posts responseEach expression runs in its own goroutine. Results are returned in order as
an array. If any expression panics, parallel re-raises the first error —
compose with try/or:
results = try parallel
`fast-command`
`slow-command`
end or err
puts "one failed: " + err
"fallback"
endUse .wait(seconds) to block with a time limit — panics on timeout:
task = spawn `sleep 10`
result = try task.wait(2) or "timed out"
puts resultThat's it! spawn gives you goroutine-powered concurrency with a clean,
Ruby-like syntax.
For producer-consumer patterns, use the queue module:
use "queue"
use "conv"
q = queue.new()
spawn
for i in [1, 2, 3]
q.push(i)
end
q.close()
end
q.each(fn(item)
puts conv.to_s(item)
end)Queues support bounded capacity (queue.new(10)), pop with timeout
(try q.pop(5) or "timeout"), and properties (q.size, q.closed).
See the queue module docs for pipelines,
backpressure, and more patterns.
See the concurrency design doc for the full specification.