Based on the docs for remotecall
I expect the WorkerPool variant to block until a worker in the pool is available for a new job. From docs:
remotecall(f, pool::AbstractWorkerPool, args...; kwargs...) -> Future
WorkerPool variant of remotecall(f, pid, ....). Wait for and take a free worker from pool and perform a remotecall on it.
So I’m confused as to why when I run the script below 10 “started” messages are printed, and then a few seconds later all the “finished” messages are printed.
using Distributed
nwork = 2
addprocs(max(0, nwork-nworkers()))
njobs = 10
ws = workers()
wp = CachingPool(ws)
rrefs = Vector{Any}(undef, njobs)
@everywhere function rfunc(i::Int)
print("starting #$i\n")
sleep(3)
print("finished #$i\n")
return i
end
for i = 1:njobs
rrefs[i] = remotecall(rfunc, wp, i)
end
Here’s the output:
julia> include("demo_pool.jl")
julia> From worker 2: starting #1
From worker 2: starting #3
From worker 2: starting #5
From worker 2: starting #7
From worker 2: starting #9
From worker 3: starting #2
From worker 3: starting #4
From worker 3: starting #6
From worker 3: starting #8
From worker 3: starting #10
From worker 2: finished #1
From worker 2: finished #3
From worker 2: finished #5
From worker 2: finished #7
From worker 2: finished #9
From worker 3: finished #2
From worker 3: finished #4
From worker 3: finished #6
From worker 3: finished #8
From worker 3: finished #10
If I do an @async
remotecall_fetch
it behaves like I intended, but I didn’t think that should be necessary. Here’s the output in that case:
julia> include("demo_pool.jl")
julia> From worker 3: starting #2
From worker 2: starting #1
From worker 2: finished #1
From worker 3: finished #2
From worker 3: starting #3
From worker 2: starting #4
From worker 3: finished #3
From worker 2: finished #4
From worker 3: starting #5
From worker 2: starting #6
From worker 3: finished #5
From worker 2: finished #6
From worker 3: starting #7
From worker 2: starting #8
From worker 3: finished #7
From worker 2: finished #8
From worker 3: starting #9
From worker 2: starting #10
From worker 3: finished #9
From worker 2: finished #10