Consider the following code which generates a closure via an @eval
(needed in my non-MWE), and then ships it to a remote worker to evaluate:
module Foo
using Distributed
make_closure(x) = @eval let x=$x; () -> x; end
function fetch_λ()
λ = make_closure(1)
@fetch λ()
end
end
Trying to use this, it errors with:
julia> using Distributed
julia> addprocs(1)
1-element Array{Int64,1}:
2
julia> using Foo
julia> Foo.fetch_λ()
ERROR: On worker 2:
UndefVarError: #3#4 not defined
However, if I switch @eval
to @eval Main
to eval the closure in Main
, then it works. Does anyone know why this is happening and/or if its a bug?
I’m aware that globals in Main are automatically shipped to workers, but here it don’t see how that applies, since λ
is a local variable inside of my fetch_λ
function. Thanks for any help.