On worker 2: UndefVarError

Viewed 214

I am trying a simple Julia code:

using Distributed

addprocs(1)

@everywhere include("count_heads.jl")

a = @spawn count_heads(100000000)

b = @spawn count_heads(100000000)


fetch(a)+fetch(b)

However the count_heads.jl contains the following function:

function count_heads(n)
c::Int = 0
for i = 1:n
    c += rand(Bool)
   end
c
end

But the above code was showing the error : On worker 2: UndefVarError: count_heads not defined

Does anyone know how to resolve this error??

1 Answers

@spawn is deprecated and you should use @spawnat :any instead:

a = @spawnat :any count_heads(100000000)

otherwise your code is perfectly correct and I managed to run it.

Most likely, in your production code you have some typo in the function name or you have run @everywhere include("count_heads.jl") before addprocs

Related