How to pass ARGS to multiple processes?

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:

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

This returns the error:

$ 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:

using Distributed
addprocs(1)
a1 = ARGS[1]
@everywhere print(a1)
$ 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.

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)…

using Distributed
a1 = ARGS[1]
addprocs(1)
@fetch a1
@everywhere print(a1)
$ julia test.jl hello
hello

You can use the exeflags argument of addprocs. Something like

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

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

1 Like

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 test.jl hello
ERROR: SystemError: opening file "hello": No such file or directory

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).

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