Define a function on a remote process only

The error stems from the fact that defining a function also gives a return value which is the function itself:

julia> function foo()
           println("HI from foo!")
       end
foo (generic function with 1 method)

so when you evaluate the function definition on the remote, it defines the function just fine, but then the return value tries to get sent back to the master process, which fails since foo isn’t defined there. You can just suppress the return value and do something like:

@everywhere pid begin
    function foo()
        println("HI from foo!")
    end
    nothing
end

(note also the @everywhere macro takes an optional pid argument so you can use the macro form)

1 Like