# How to pass ARGS to multiple processes?

**URL:** <https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075>\
**Category:** General Usage\
**Tags:** distributed, argument\
**Created:** [April 26, 2022, 4:21pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075 "2022-04-26T16:21:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JedPhillips](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@JedPhillips](https://discourse.julialang.org/u/JedPhillips)\
**Post date:** [April 26, 2022, 4:21pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075/1 "2022-04-26T16:21:24Z")

</div>

I’m trying to pass command line arguments to a script that calls multiple processes, but I can’t seem to have the other processes access these arguments. Here’s a simple example:

```julia
using Distributed
addprocs(1)
@everywhere a1 = ARGS[1]
@everywhere print(a1)

```

This returns the error:

```julia
$ julia test.jl hello
ERROR: LoadError: On worker 2:
BoundsError: attempt to access 0-element Vector{String} at index [1]

```

If I edit this to remove the @everywhere when saving the argument, I get a different error:

```julia
using Distributed
addprocs(1)
a1 = ARGS[1]
@everywhere print(a1)

```

```julia
$ julia test.jl hello
ERROR: LoadError: On worker 2:
UndefVarError: a1 not defined

```

I’m sure this is just simple syntax I’m missing, so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![JedPhillips](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@JedPhillips](https://discourse.julialang.org/u/JedPhillips)\
**Post date:** [April 26, 2022, 4:29pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075/2 "2022-04-26T16:29:15Z")

</div>

Answered my own question --Got it to work using the @fetch command. EDIT. Actually this only works when adding a single proc, but fails with addprocs(2)…

```julia
using Distributed
a1 = ARGS[1]
addprocs(1)
@fetch a1
@everywhere print(a1)

```

```julia
$ julia test.jl hello
hello

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [April 26, 2022, 4:29pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075/3 "2022-04-26T16:29:50Z")

</div>

You can use the `exeflags` argument of `addprocs`. Something like

```julia
addprocs(1, exeflags="$(ARGS[1]), $(ARGS[2])")

```

This will let the worker processes see the variable `ARGS` as well.

---

<div class="post-metadata">

**Author:** ![JedPhillips](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@JedPhillips](https://discourse.julialang.org/u/JedPhillips)\
**Post date:** [April 26, 2022, 4:39pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075/4 "2022-04-26T16:39:49Z")

</div>

Thanks for the suggestion. This doesn’t quite seem to be doing the right thing, though I don’t fully undersand the exeflags to get why. It’s trying to access a file:

```julia
$ julia test.jl hello
ERROR: SystemError: opening file "hello": No such file or directory

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [April 26, 2022, 6:17pm UTC](https://discourse.julialang.org/t/how-to-pass-args-to-multiple-processes/80075/5 "2022-04-26T18:17:54Z")

</div>

Interesting. I thought that would work since `addprocs` is simply just launching processes with this: `julia --worker` so adding `exeflags="hello"` would look like `julia --worker hello`.

You could just define a function on all your worker processes and pass in ARGS (notice the interpolation).

```julia
@everywhere function myfunc(funcARGS)
        println("from $(myid()) ARGS: $funcARGS")
end
@everywhere myfunc($ARGS)

```
