Skip to content

Process a list in parallel with a limit ​

Run one piece of work per element at the same time, but never more than a fixed number at once, and get the results back in input order. The examples fetch pages from a web server whose address is in base, such as http://127.0.0.1:8080.

ant
func fetch(path: string, work: Context): Result[string, Error] {
  let response = try Http.get("${base}${path}", ctx: work)
  if response.status != 200 {
    return Result.fail("${path}: status ${response.status}")
  }
  Result.ok(response.text())
}

let paths = ["/pages/home", "/pages/about", "/pages/blog", "/pages/contact", "/pages/faq"]
let deadline = Context.new(10s)
let results = Parallel.map(paths, fetch, limit: 3, ctx: deadline)
if results == null {
  return Result.fail("gave up: ${deadline.reason() ?? "cancelled"}")
}
for page in try (results | Result.all()) {
  println(page)
}
text
contents of /pages/home
contents of /pages/about
contents of /pages/blog
contents of /pages/contact
contents of /pages/faq
  1. Write the work for one element as a function that takes the element and a Context. Pass the context to every wait inside it, here Http.get, so the work stops when the batch is cancelled. Pass the callback's own context, work, not deadline: only work ends when a sibling panics. Return Result.fail for a failure and Result.ok of Http.text for a page.
  2. Call Parallel.map with limit:, the most callbacks that run at once. Here at most three requests are in flight; the next path starts when one of them ends. limit must be at least 1; a smaller value panics. Choose the limit for what the work waits on. For requests, pick what the server can take. For work that keeps a CPU busy, such as hashing or parsing, pass Os.cpus(): more callbacks than CPUs only wait for one.
  3. Bound the whole batch with a deadline from Context.new, passed as ctx:. Parallel.map returns null when the context ends before every element has started, and Context.reason says why.
  4. Turn the array of results into one Result with Result.all: the first Err in input order, or Ok of every page.

Parallel.map returns only after every callback has ended, and the results keep the order of paths, whatever order the requests finish in. If a callback panics, the others are cancelled and the call panics with the same message.

Give up after a deadline ​

When the deadline passes while elements are still waiting for a slot, those elements never start, the running callbacks see their context end, and the call returns null:

ant
let paths = ["/pages/home", "/slow/archive", "/pages/about", "/slow/search", "/pages/faq"]
let deadline = Context.new(100ms)
let results = Parallel.map(paths, fetch, limit: 2, ctx: deadline)
if results == null {
  println("gave up: ${deadline.reason() ?? "cancelled"}")
}
text
gave up: timeout

A deadline that passes after the last element has started does not give null: the waits in the running callbacks give up, so Http.get returns Err with the message timeout, and the array holds that Err.

Keep the results that finished ​

Parallel.map gives all results or null. To keep what finished before the deadline, use Parallel.each over Array.entries and write each result into its own slot of an array made with Array.filled. The call returns after every callback has ended, so you can read the slots without a lock. A slot still null is an element that never started:

ant
let paths = ["/pages/home", "/slow/archive", "/pages/about", "/slow/search", "/pages/faq"]
let deadline = Context.new(100ms)
let pages: Array[Result[string, Error] | null] = Array.filled(paths.length(), null)
Parallel.each(paths.entries(), func(entry: Indexed[string], work: Context) {
  pages[entry.index] = fetch(entry.value, work)
}, limit: 2, ctx: deadline)
for entry in pages.entries() {
  let path = paths[entry.index]
  let page = entry.value
  if page == null {
    println("${path}: not started")
  } else if page is Ok {
    println("${path}: ${page.value}")
  } else {
    println("${path}: ${page.error.message}")
  }
}
text
/pages/home: contents of /pages/home
/slow/archive: timeout
/pages/about: contents of /pages/about
/slow/search: timeout
/pages/faq: not started

Stop at the first failure ​

An Err from one callback stops nothing: the other elements still run. To stop the batch at the first failure, pass a context of your own as ctx: and cancel it with Context.cancel when a callback fails. Record the error in an Atomic made with Atomic.new, since several callbacks can fail at once. Atomic.compareAndSwap keeps only the first error, and Atomic.load reads it after the call:

ant
let paths = ["/pages/home", "/missing", "/pages/about", "/pages/faq"]
let stop = Context.new()
let firstError: Atomic[Error | null] = Atomic.new(null)
let results = Parallel.map(paths, func(path: string, work: Context): Result[string, Error] {
  let page = fetch(path, work)
  if page is Err {
    firstError.compareAndSwap(null, page.error)
    stop.cancel()
  }
  page
}, limit: 2, ctx: stop)
if results == null {
  println("stopped: ${firstError.load()?.message}")
}
text
stopped: /missing: status 404

The request for /pages/home was running when /missing failed, so its context ended too. /pages/about and /pages/faq never started.

See also ​

Parallel.map · Parallel.each · Context.new · Os.cpus · Run a worker pool over a channel · Give up after a timeout or deadline