Playground · Concurrency · 32 of 33
tasks
Tasks: run work at the same time, inside a group that waits for it all.
# Tasks: run work at the same time, inside a group that waits for it all. def slow_square(n : Int32) : Int32 sleep 10 # milliseconds n * n end # `group` opens a scope. Every `spawn` inside it is a task, and the # group's `end` waits for every one of them: no task outlives its group. results = [] of Int32 group do |g| (1..4).each do |n| g.spawn do results << slow_square(n) 0 end end end puts results.sorted # `end!` turns the group into a value: the tuple of what its tasks answered. def both : Tuple(String, String) | Panicked group do |g| a = g.spawn { "first" } b = g.spawn { "second" } end! end case both in Tuple(String, String) then puts it in Panicked then puts "a task panicked" end
[1, 4, 9, 16]
{"first", "second"}
exit 0, on Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+
The pane no longer holds samples/tour/tasks.iyi. What the output pane shows is the recorded run of the recorded file, not of what you typed. How this works
recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19
This program does not compile for the browser's target, so this page shows what it printed when the compiler ran it natively, on the machine named above. You can still type in the pane, and the page will say when what is in it is no longer the file the recording is about.
Tasks and channels run on Linux, macOS arm64 and Windows x86-64, and not on wasm32 yet: a program that names `group` or `Channel` there does not compile, because src/iyi/concurrency.iyi refuses to ship a spelling without the feature (SPEC.md III.4.8). What you see is what the program printed when the compiler ran it natively. How this works
Try it on your machine
Copy the program into tasks.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.
$ iyi run tasks.iyi