Doing things at once#
Orders arrive as one file per day. Reading them one after another wastes time when each read waits on the disk. In Manuscript a function that waits is an ordinary function, so there is no async to add. A call that waits returns a Promise, and you can start several before you collect any.
rows.ms
import (
wait_all from "@std/concurrency/promise.ms",
read_file from "@std/os/fs.ms",
)
fn count_rows(text String) Int {
return text.split("\n").filter(line => not line.is_empty()).length()
}
fn run(paths List[String]) Int! {
reads := paths.map(path => read_file(path))
files := wait_all(reads)?
var rows = 0
for file in files {
text := ok_or(file.decode_utf8(), Error("not UTF-8", kind: "EBADMSG"))?
rows += count_rows(text)
}
return ok(rows)
}
fn main() Void! {
rows := run(["a.csv", "b.csv"])?
print(f"{rows} rows from 2 files")
}text
3 rows from 2 filesSave it as rows.ms, with a.csv and b.csv beside it, and run ms run rows.ms.
What to notice#
read_file(path)returns aPromisethat is already running.paths.map(...)starts both reads, andwait_allcollects the results in order.- Nothing is marked
async.runis a plain function that happens to wait. - The first failure wins. If a file is missing,
wait_allreturns that error and cancels the reads still in flight. - A promise that is dropped before it finishes is cancelled, and its work ends before the values it uses go away. Work never outlives its owner.
- Cancellation arrives at the next wait, and cleanup runs as it does on any other exit.
Sharing data#
A promise reads what it captures and cannot change it. When several tasks need to update one value, give that value one owner and send it messages through a pipe . Ownership is the reason there is no data race to guard against.
Next: Organizing code . Exact rules: Promises , Streams and pipes .