How to understand code availability of @spawn



In official documentation, two examples are used:

julia> function rand2(dims...)
           return 2*rand(dims...)

julia> rand2(2,2)
2×2 Array{Float64,2}:
 0.153756  0.368514
 1.15119   0.918912

julia> fetch(@spawn rand2(2,2))
ERROR: RemoteException(2, CapturedException(UndefVarError(Symbol("#rand2"))
julia> A = rand(1000,1000);

julia> Bref = @spawn A^2;


julia> fetch(Bref);

These two examples seem to be very similiar. However, in the first example, rand2 is not available on worker’s process while in the second example, A is available on worker’s process. The only difference is rand2 is a function and A is a variable. What’s the essential difference between a function and a variable such that they behave differently in macro @spawn?


Sorry, I noticed immediately the following section says Global variables are treated differently.

How can I delete the post?