Run a worker pool over a channel
Hand jobs to a fixed number of workers as a producer finds them. This script reads file paths from standard input, as find logs -name '*.log' | ant run count.ant gives them, and counts the lines of each file with at most four files open at once:
func countLines(path: string): string {
let text = File.read(path)
if text is Err { return text.error.message }
"${text.value.lines().length()} lines"
}
let jobs: Channel[string] = Channel.new()
go {
defer { jobs.close() }
let input = Os.stdin()
while true {
let path = input.readLine().unwrapOr(null) ?? break
jobs.send(path)
}
}
let m = Mutex.new()
let reports: Map[string, string] = Map{}
Parallel.each(jobs, func(path: string, work: Context) {
let report = countLines(path)
m.withLock({ reports[path] = report })
}, limit: 4)
for path in reports.keys() | Array.sort() {
println("${path}: ${reports[path]}")
}logs/api.log: 3 lines
logs/db.log: 2 lines
logs/missing.log: open logs/missing.log: no such file or directory
logs/web.log: 2 lines- Make the job channel with Channel.new. Annotate its type, since nothing else fixes it.
- Start the producer with
go. Send each job with Channel.send, and close the channel with Channel.close in adefer, so the workers end even when the producer stops early. Here the producer reads lines with File.readLine and stops at the end of the input or at a read error. - Run the workers with Parallel.each over the channel.
limit:is the number of workers. The call takes a job only when a worker is free, and returns once the channel is closed and empty and every job has finished. Here four bounds the files open at once. For jobs that keep a CPU busy, useOs.cpus()workers. - Collect the results in a map guarded by Mutex.withLock, since several workers write it at once. Read it after
Parallel.eachreturns, with no lock.
The jobs finish in no fixed order, so sort the results with Array.sort before you print them. A panic in a job ends the pool and is raised again where Parallel.each was called.
Stop the pool at the first failure
Make a context with Context.new and give it to both sides: to Parallel.each as ctx:, and to each Channel.send of the producer. A job that fails calls Context.cancel. The pool then takes no more jobs, and the producer's send returns false instead of waiting for a worker that never comes:
let ctx = Context.new()
let jobs: Channel[string] = Channel.new()
go {
defer { jobs.close() }
for path in ["logs/api.log", "logs/missing.log", "logs/db.log"] {
if !jobs.send(path, ctx: ctx) { break }
}
}
let failure: Atomic[Error | null] = Atomic.new(null)
Parallel.each(jobs, func(path: string, work: Context) {
let text = File.read(path)
if text is Err {
failure.compareAndSwap(null, text.error)
ctx.cancel()
}
}, limit: 4, ctx: ctx)
let err = failure.load()
if err != null {
println("stopped: ${err.message}")
}stopped: open logs/missing.log: no such file or directoryAtomic.compareAndSwap keeps the first error when several jobs fail at once. A job that has already started runs to its end; pass work to its own waits so they end when the pool stops.
Write the pool with go and WaitGroup
Start the workers yourself when they send their results on to another channel. Each worker loops over Channel.values until the job channel is closed and empty. A WaitGroup counts the workers, and a last goroutine closes the results channel once they have all ended. A file that fails to read adds nothing to the total:
let jobs: Channel[string] = Channel.new()
go {
defer { jobs.close() }
for path in ["logs/api.log", "logs/db.log", "logs/web.log"] {
jobs.send(path)
}
}
let results: Channel[int] = Channel.new()
let workers = 4
let wg = WaitGroup.new()
wg.add(workers)
for _ in Iter.range(0, workers) {
go {
defer { wg.done() }
for path in jobs.values() {
let text = File.read(path)
if text is Err { continue }
results.send(text.value.lines().length())
}
}
}
go {
wg.wait()
results.close()
}
var total = 0
for n in results.values() {
total += n
}
println("${total} lines")7 lines- Call WaitGroup.add with the number of workers before you start them.
- Call WaitGroup.done in a
deferat the top of each worker, so a worker that panics still counts as ended. - Close the results channel from a goroutine that calls WaitGroup.wait. The main goroutine reads the results meanwhile, so no worker waits on a send that nobody takes.
Unlike Parallel.each, a panic here ends only the worker it occurs in, and the others go on. To keep that worker taking jobs, run each job through Panic.catch.
See also
Channel.new · Channel.send · Channel.close · Channel.values · Channel.receive · Parallel.each · WaitGroup · Context.new · Context.cancel · Mutex.withLock · Atomic.compareAndSwap · Os.stdin · Os.cpus · File.readLine · File.read · Process a list in parallel · Cancel a group of goroutines · Concurrency
