Hi
Consider the following two files test.jl
and temp.jl
with the following content
# test.jl file
module Foo
using Distributed
function baz()
addprocs(2)
@everywhere include("temp.jl")
for worker in workers()
remotecall_fetch(println, worker, bar)
end
end
function baz2()
addprocs(2)
@everywhere include("temp.jl")
for worker in workers()
remotecall_fetch(println, worker, Main.bar)
end
end
end # module
# temp.jl
bar(x) = 2x
When I run the following
julia> import .Foo
julia> using Distributed
julia> @fetchfrom workers()[1] InteractiveUtils.varinfo() # bar is in varinfo
name size summary
––––––––––– ––––––––– ––––––––––––––––––––––––––––––––––––
Base Module
Core Module
Distributed 1.079 MiB Module
Main Module
bar 0 bytes bar (generic function with 1 method)
I see that bar
is loaded in the worker process. While the following throws an UndefVarError
julia> Foo.baz()
ERROR: UndefVarError: bar not defined
Stacktrace:
[1] baz()
@ Main.Foo ~/.julia/dev/DynamicalNetworks/ClusterShiftKeyinChaosCommunication/test.jl:22
[2] top-level scope
@ REPL[1]:1
the following throws no error.
julia> Foo.baz2()
From worker 2: bar
From worker 3: bar
From worker 4: bar
From worker 5: bar
From worker 6: bar
From worker 7: bar
Thus, it seems that bar
cannot be used without specifying the module name Main
explicitly. I understand that bar
is bound to module Main
. But, why do I have to specify it when I try to reach the bound function bar
?
Moreover, if I define function not inside of a module, the prefix Main
is not required again as shown below. I could not get why. Any help is appreciated.
julia> function baz3()
addprocs(2)
@everywhere include("temp.jl")
for worker in workers()
remotecall_fetch(println, worker, bar)
end
end
baz3 (generic function with 1 method)
julia> baz3()
From worker 2: bar
From worker 3: bar
From worker 4: bar
From worker 5: bar
From worker 6: bar