我正在尝试一个简单的朱莉娅代码:
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 2: UndefVarError: count_heads未定义
有人知道如何解决这个错误吗?
发布于 2021-06-17 22:03:57
不建议使用@spawn,而应该使用@spawnat :any:
a = @spawnat :any count_heads(100000000)否则,您的代码是完全正确的,我设法运行它。
最有可能的是,在您的生产代码中,函数名有一些错误,或者在@everywhere include("count_heads.jl")之前运行了addprocs。
https://stackoverflow.com/questions/68026300
复制相似问题