2017-08-30 46 views
0

说我有这样的如何从@everywhere包括(ITER)的循环

function paths(loc) 
    if loc[end] == "/" 
     loc = loc[1:end-1]  
    end 
    [string(loc, script) for script in 
    ["/s1.jl", "/s2.jl", "/s3.jl"]] 
end 

的功能,我想这样做:

for p in paths("/some/path") 
    @everywhere include(p) 
end 

我得到的错误:

ERROR: UndefVarError: p not defined 
eval(::Module, ::Any) at ./boot.jl:235 
eval_ew_expr at ./distributed/macros.jl:116 [inlined] 
(::Base.Distributed.##135#136{Base.Distributed.#eval_ew_expr,Tuple{Expr},Array{Any,1}})() at ./distributed/remotecall.jl:314 
run_work_thunk(::Base.Distributed.##135#136{Base.Distributed.#eval_ew_expr,Tuple{Expr},Array{Any,1}}, ::Bool) at ./distributed/process_messages.jl:56 
#remotecall_fetch#140(::Array{Any,1}, ::Function, ::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:339 
remotecall_fetch(::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:339 
#remotecall_fetch#144(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:367 
remotecall_fetch(::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:367 
(::##189#191)() at ./distributed/macros.jl:102 
#remotecall_fetch#140(::Array{Any,1}, ::Function, ::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:340 
remotecall_fetch(::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:339 
#remotecall_fetch#144(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:367 
remotecall_fetch(::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:367 
(::##189#191)() at ./distributed/macros.jl:102 
Stacktrace: 
[1] sync_end() at ./task.jl:287 
[2] macro expansion at ./distributed/macros.jl:112 [inlined] 
[3] macro expansion at ./REPL[33]:2 [inlined] 
[4] anonymous at ./<missing>:? 

问题是,如果这是可能的,我该如何得到这个工作?

+4

用'@everywhere include($ p)'替换'@everywhere include(p)'。 '$'是插值,它将表达式参数中的值'p'替换为'@ everywhere'。 –

+1

嘿@DanGetz,你可以发表相同的答案:-)这种方式不会在没有答案的问题中可见。 –

+1

@RahulLakhanpal发表 –

回答

1

@everywhere include(p)替换为@everywhere include($p)$是插值,它将p符号替换为表达式参数中的值为@everywhere