How may I send data to other processes within a function?

Hello, I would like to send some data to other processes in a function, however, it is not successful. Only when I @spawnat in the Main module is it able to show up on the var table and be accessible within @everywhere. I must be missing something here. Thank you in advance!

julia> using Distributed

julia> addprocs(4)

julia> a = 123456;

julia> @everywhere using InteractiveUtils

julia> function send_my_data(data)
           @sync begin
               for p in workers()
                   @async @spawnat p data
               end
           end
       end

julia> send_my_data(a)

# sending the data fails as from var table
julia> @fetchfrom 2 varinfo()
name size summary
–––– –––– –––––––
Base      Module 
Core      Module 
Main      Module 

# This indicates no `a` existing on other processes
julia> @everywhere begin a end
ERROR: On worker 2:
UndefVarError: a not defined ...

# However, this shows there is an `a` on other processes
julia> remotecall_fetch(()->a, 2)
123456

Check out https://github.com/ChrisRackauckas/ParallelDataTransfer.jl/

2 Likes

Thank you, I wasn’t aware of this tools before.