From 98562454d8e2c9c4b510dca996f8af5eeb305474 Mon Sep 17 00:00:00 2001 From: Frames White Date: Wed, 4 Feb 2026 16:36:10 +1100 Subject: [PATCH] Add a do-block form of the CachingPool This is just making https://github.com/JuliaLang/julia/pull/27683 again. Since the code moved to a new repo --- src/workerpool.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/workerpool.jl b/src/workerpool.jl index 6cd355e..639caab 100644 --- a/src/workerpool.jl +++ b/src/workerpool.jl @@ -371,6 +371,28 @@ function CachingPool(workers::Vector{Int}) return pool end +""" + CachingPool(func, workers::Vector{Int}) + +Excute `func(pool)`, clearing out any cached functions from the workers when complete. + +# Examples +```julia +CachingPool(workers()) do wp + foo = rand(10^8); + pmap(wp, i -> sum(foo) + i, 1:100); +end +""" +function CachingPool(func, workers::Vector{Int}) + pool = CachingPool(workers) + try + func(pool) + finally + clear!(pool) + end +end + + """ clear!(pool::CachingPool) -> pool