Julia bounded worker pool with error capture
Julia
function pool_map(f, inputs; workers=4)
jobs = Channel{Tuple{Int,Any}}(length(inputs))
foreach(x -> put!(jobs, x), collect(enumerate(inputs)))
close(jobs)
results, errors = Vector{Any}(fill(nothing, length(inputs))), Tuple{Int,Any}[]
guard = ReentrantLock()
Threads.@threads for _ in 1:workers # workers drain the shared channel
for (i, x) in jobs
try
results[i] = f(x)
catch err
lock(guard) do
push!(errors, (i, err))
end
end
end
end
return results, errors
end
println(pool_map(x -> x^2, 1:5; workers=2))