我制作了一个带有if条件的模块,该模块的核心数量为如果核心数大于1,则路由为并行;否则,它将遵循下面的代码
module mymodule
import Pkg
using Distributed
if nworkers() > 1
@everywhere using Pkg
@everywhere Pkg.activate(".")
@everywhere Pkg.instantiate()
@everywhere using CSV
@everywhere include("src/myfuncs.jl")
function func()
df=CSV.read(file);
.......
end
else
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using CSV
include("src/myfuncs.jl")
function func()
df=CSV.read(file);
.......
end
end
end #mymodule
1)当我实例化Julia会话时,例如julia -p 8
,我收到一条错误消息,说ERROR: UndefVarError: CSV not defined
。另一方面,简单地将会话实例化为julia
时没有错误。 Project.toml和Master.toml文件比src
高一级。在使用@everyone
之类的
include("src/myfuncs.jl")
@everywhere include("src/myfuncs.jl")
2)此外,我发现当程序进行串行路由时,找不到myfunc.jl
文件,因为该文件已经在src
文件夹中(寻找src/src/myfunc.jl
) ,这种行为使我感到困惑。
有人可以在这里分享他们的想法吗?
答案 0 :(得分:3)
@everywhere
执行对所有工人和主服务器执行。但是:
有时,如果运气不好并且正在导入的模块未编译,则可能会发生竞争状况(并非总是可复制的,但由StackOverflow上的多个用户报告),因此,最好的选择是始终编写这样的代码(请注意,如果您的集群分布在许多服务器上,可能还不够):
using Distributed
@everywhere using Distributed
using CSV
@everywhere using CSV
修改代码以运行using
之前 Pkg.activate
我不确定您要@everywhere Pkg.instantiate()
实现什么,但是不能确定您现在正在做什么,这不好(您必须确保它不会在多个副本中运行)群集节点)
最后,无需根据工作人员的数量来分离代码-请参阅第(1)点中的安全模式
希望有帮助!