【问题标题】:On worker 2: UndefVarError在工人 2 上:UndefVarError
【发布时间】:2021-06-18 06:03:57
【问题描述】:

我正在尝试一个简单的 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

但上面的代码显示错误: 在工人 2 上: UndefVarError: count_heads 未定义

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

【问题讨论】:

    标签: julia


    【解决方案1】:

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

    a = @spawnat :any count_heads(100000000)
    

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

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

    【讨论】:

    • 它仍然显示相同的错误。我使用了 '@spawnat :any' 并在 'addprocs' 之后声明了 '@everywhere include("count_heads.jl")'。
    • 我已经运行了你的代码,它可以正常工作。它对我来说也是正确的(上面有 cmets)。你使用的是什么 Julia 版本(尽管这段代码至少对于 Julia 1.0 来说看起来非常好)
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多