How to get persistence on distributed workers

It’s failing because you are trying to assign to A a brand new RHS. Typing in A = ... dosn’t “mutate” A. It tries to redefine the variable A to whatever the RHS is, which is not allowed from another module. (The old binding is actually lost and likely garbage collected). See this thread on defining variables in other modules.

You can fix this by indeed mutating the variable with the broadcast notation, i.e. @everywhere testmoda.A = [1:nworkers();] like your first test. If you want to test mutability without broadcast, use a for loop or some other mechanism.

for i in eachindex(A) 
   A[i] = nworkers()[i]
end

although again, I imagine this is for learning purposes as there isn’t any difference between the broadcast code vs the for loop.

Also note that ismutable(testmoda.A) is fine. It’s not redefining A, and indeed tells you at whatever A is pointing to can be modified.

1 Like