# Loading code in worker processes

**URL:** <https://discourse.julialang.org/t/loading-code-in-worker-processes/21471>\
**Category:** New to Julia\
**Created:** [March 4, 2019, 5:21pm UTC](https://discourse.julialang.org/t/loading-code-in-worker-processes/21471 "2019-03-04T17:21:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhazeghi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhazeghi/32/7107_2.png) [@dhazeghi](https://discourse.julialang.org/u/dhazeghi)\
**Post date:** [March 4, 2019, 5:21pm UTC](https://discourse.julialang.org/t/loading-code-in-worker-processes/21471/1 "2019-03-04T17:21:56Z")

</div>

Previously I’ve always specified the number of worker processes on the Julia command line via -p . Since the source file passed in is the same for all the processes, I can use Distributed.remotecall\_fetch() for any function available to the main process.

Now I’m trying to add workers dynamically, via addproc from julia\_main() in program.jl (the entry-point to my application). I’d like to be able to do the equivalent of `@everywhere import MyModule` once the workers have been added (in julia\_main()), but ‘import’ can only be used at the top level. So far, the solution I’ve come up with is to do `@everywhere include("/path/to/program.jl")` but this seems rather ugly. Is there a cleaner way? Thanks.

Dara

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 4, 2019, 5:23pm UTC](https://discourse.julialang.org/t/loading-code-in-worker-processes/21471/2 "2019-03-04T17:23:59Z")

</div>

`eval()` and `@eval` always execute at the top level (and in global scope). Does `@eval import MyModule` work for your case?

---

<div class="post-metadata">

**Author:** ![dhazeghi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhazeghi/32/7107_2.png) [@dhazeghi](https://discourse.julialang.org/u/dhazeghi)\
**Post date:** [March 6, 2019, 3:23am UTC](https://discourse.julialang.org/t/loading-code-in-worker-processes/21471/3 "2019-03-06T03:23:43Z")

</div>

Yep, that does exactly what I need. This also works with static compilation which failed with the `@everywhere include` hack. Thanks a bunch.

---

<div class="post-metadata">

**Author:** ![dhazeghi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhazeghi/32/7107_2.png) [@dhazeghi](https://discourse.julialang.org/u/dhazeghi)\
**Post date:** [March 7, 2019, 6:00pm UTC](https://discourse.julialang.org/t/loading-code-in-worker-processes/21471/4 "2019-03-07T18:00:55Z")

</div>

An interesting wrinkle is that if one attempts to combine the module and the application driver in the same file, this fails, because the ``@everywhere @eval import Module``` occurs in the file defining the module, hence it is already defined in the main process.

I tried a number of different workarounds, but the best I could come up with was:

```julia
        addprocs(n)
        ...
        @everywhere if !isdefined(Main, :Module) # Shouldn't import into the main worker
            @eval import Module
        end

```
