首页 > 解决方案 > 在工人 2 上:UndefVarError

问题描述

我正在尝试一个简单的 Julia 代码:

using Distributed

addprocs(1)

@everywhere include("count_heads.jl")

a = @spawn count_heads(100000000)

b = @spawn count_heads(100000000)


fetch(a)+fetch(b)

但是 count_heads.jl 包含以下函数:

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

但是上面的代码显示了错误: On worker 2: UndefVarError: count_heads not defined

有谁知道如何解决这个错误??

标签: julia

解决方案


@spawn已弃用,您应该@spawnat :any改用:

a = @spawnat :any count_heads(100000000)

否则你的代码是完全正确的,我设法运行它。

很可能,在您的生产代码中,函数名称中有一些拼写错误,或者您之前运行@everywhere include("count_heads.jl")addprocs


推荐阅读