Parallel
The Parallel module provides synchronous-looking parallel operations that run on separate threads. Unlike async/await patterns, these functions block until completion, making code easier to reason about while still benefiting from parallel execution.
Parallel execution requires ParallelAuth, ConcurrencyAuth, or
RootAuth in scope. Resource-limit helpers use the same concurrency authority checks.
Kit avoids the "colored function" problem. Parallel operations look and behave like normal function calls - no async/await, no special syntax. The parallelism is transparent.
Basic Execution
Ok result on success, Err message on failure.
result = Parallel.run (fn => expensive-computation 42)
match result
| Ok value -> println "Result: ${value}"
| Err msg -> println "Failed: ${msg}"
Some (Ok result) on success, Some (Err msg) on failure,
or None if the timeout expires.
match Parallel.run-timeout 5000 (fn => slow-operation)
| Some (Ok value) -> println "Got: ${value}"
| Some (Err msg) -> println "Failed: ${msg}"
| None -> println "Timed out after 5 seconds"
Parallel Mapping
urls = ["https://a.com", "https://b.com", "https://c.com"]
# Fetch all URLs in parallel
results = Parallel.map urls (fn(url) => HTTP.get! url)
# Process results
results |> List.each (fn(r) =>
match r
| Ok body -> println "Got ${String.length body} bytes"
| Err e -> println "Failed: ${e}"
)
Multiple Tasks
results = Parallel.all [
fn => task1,
fn => task2,
fn => task3
]
println "All tasks completed: ${results}"
# Return whichever completes first
result = Parallel.first [
fn => fetch-from-primary,
fn => fetch-from-backup
]
# Try multiple sources, return first success
result = Parallel.any [
fn => might-fail-1,
fn => might-fail-2,
fn => should-work
]
match result
| Ok value -> println "Got result: ${value}"
| Err msg -> println "All failed: ${msg}"
Error Types
type ParallelError =
| SpawnError { message: String }
| TimeoutError
| TaskError { message: String }
| InvalidFunction
| ParallelError { message: String }
Complete Example
# Fetch multiple URLs with timeout
fetch-with-timeout = fn(url) =>
Parallel.run-timeout 5000 (fn => HTTP.get url)
# Fetch all URLs in parallel
urls = [
"https://api.example.com/users",
"https://api.example.com/posts",
"https://api.example.com/comments"
]
results = Parallel.map urls fetch-with-timeout
# Process results
results |> List.zip urls |> List.each (fn((url, result)) =>
match result
| Some (Ok response) -> println "${url}: OK"
| Some (Err msg) -> println "${url}: Error - ${msg}"
| None -> println "${url}: Timeout"
)