# Loading packages to the freshly added processes

**URL:** <https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434>\
**Category:** Julia at Scale\
**Created:** [May 3, 2022, 3:54pm UTC](https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434 "2022-05-03T15:54:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [May 3, 2022, 3:54pm UTC](https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434/1 "2022-05-03T15:54:08Z")

</div>

I am trying to write the function that launches multithreaded processes and then pins the threads to the core.  
The stripped down version looks like

```julia
using Distributed, ThreadPinning
function add_pinned_workers(nw::Int, nt::Int, coress)
    worker_list = addprocs(nw, exeflags=["--threads=$nt", "--project=$(Base.active_project())"])
    @sync for wid in worker_list
        @spawnat wid ThreadPinning.pinthreads(worker_list[wid-1])
    end 
end

```

Now, on a 8 core machine I can, for example, run `add_pinned_workers(2,4, [[0,1,2,3],[4,5,6,7]])`. However, this code does not execute, because `ThreadPinning` is not loaded on the newly added processes:

> KeyError: key ThreadPinning [811555cd-349b-4f26-b7bc-1f208b848042] not found

Essentially, I need somehow to add `ThreadPinning` to the freshly spawned workers in the same function call. I have tried adding something like `using ThreadPinning` inside the function, but it is not allowed:

> ERROR: LoadError: syntax: “using” expression not at top level

**So, how to actually use “using” inside a function? Or do something similar in effect?**

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 3, 2022, 4:47pm UTC](https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434/2 "2022-05-03T16:47:49Z")

</div>

```julia
julia> function load_packages()
           @eval begin
               using Interpolations
           end
       end
load_packages (generic function with 1 method)

julia> load_packages()

julia> Interpolations
Interpolations

```

See [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Base.@eval)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 3, 2022, 4:49pm UTC](https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434/3 "2022-05-03T16:49:10Z")

</div>

You may also be interested in [`Distributed.@everywhere`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.@everywhere).

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [May 3, 2022, 5:49pm UTC](https://discourse.julialang.org/t/loading-packages-to-the-freshly-added-processes/80434/4 "2022-05-03T17:49:07Z")

</div>

Thank you, guys.  
If someone else is interested, the following code works:

```julia
using Distributed, ThreadPinning
function add_pinned_workers(nw::Int, nt::Int, coress)
    worker_list = addprocs(nw, exeflags=["--threads=$nt", "--project=$(Base.active_project())"])
    @eval @everywhere using ThreadPinning
    @sync for wid in worker_list
        @spawnat wid ThreadPinning.pinthreads(worker_list[wid-1])
    end 
end

```
