# Distributed loop and "include"

**URL:** <https://discourse.julialang.org/t/distributed-loop-and-include/52886>\
**Category:** General Usage\
**Tags:** parallel, distributed\
**Created:** [January 5, 2021, 4:56pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886 "2021-01-05T16:56:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [January 5, 2021, 4:56pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886/1 "2021-01-05T16:56:59Z")

</div>

Hello. My question is how I include an external file within a distributed loop.

For example, the following works:

```julia
using Distributed

addprocs(2);

#Parameters:
par1 = 5;
par2 = 2;
@everywhere begin
    using CSV, DataFrames;
end
@everywhere mod_pars = Dict("par1"=>$par1,"par2"=>$par2);
@everywhere par_values = collect(1:10);

@distributed for iv = 1:10
    mod_pars["par1"] = par_values[iv];
    df_tmp = DataFrame();
    df_tmp[!,:values1] = ones(10)*mod_pars["par1"];
    df_tmp[!,:values2] = ones(10)*mod_pars["par2"];
    CSV.write(joinpath("figures","data_"*string(iv)*".csv"),df_tmp);    
end

rmprocs(workers())

```

but including what’s inside the distributed loop into a separate file, called `new_file.jl`, will not work:

```julia
@distributed for iv = 1:10
    mod_pars["par1"] = par_values[iv];
    include("new_file.jl")
end

```

Is there some syntax I am getting wrong?

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [January 5, 2021, 5:14pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886/2 "2021-01-05T17:14:52Z")

</div>

You have to write `@everywhere include("new_file.jl")` so that the file’s content is available on all workers.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 5, 2021, 5:19pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886/3 "2021-01-05T17:19:07Z")

</div>

what does the error say, this use of `include` is an anti-pattern and I don’t know if it should work. Maybe your pathing for workers are different?

---

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [January 5, 2021, 5:19pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886/4 "2021-01-05T17:19:20Z")

</div>

That doesn’t seem to work. What I found I can do is wrap it a function and then call that function (after including that function @everywhere).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 5, 2021, 5:19pm UTC](https://discourse.julialang.org/t/distributed-loop-and-include/52886/5 "2021-01-05T17:19:56Z")

</div>

[https://stackoverflow.com/questions/41288626/what-exactly-does-include-do](https://stackoverflow.com/questions/41288626/what-exactly-does-include-do)

* * *

the real question is why do you want to put some script code (parts of a for loop) into a file. I suggest the following pattern:

```julia
@everhwer par1, par2 ...
@distributed for iv = par_values
   mod_pars = Dict("par1"=>par1,"par2"=>par2);
   do_and_write_CSV(mod_pars, iv)    
end

```
