Last active
January 18, 2021 22:16
-
-
Save patrick-kidger/4e4aad53189382f779e3f857030b83b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Distributed | |
foo_quote = quote | |
function foo(name::String)::String | |
id = Distributed.myid() | |
return "Hello $name from $id" | |
end | |
end | |
function run(num_procs::Integer)::Array{String} | |
results = [] | |
procs = Distributed.addprocs(num_procs) | |
try | |
# Define functionality | |
# | |
# There's lots of equivalent ways of doing this. | |
# I think probably the easiest is actually to put | |
# the other definitions all in one file, and then | |
# doing @spawnat proc include("file_defining_foo.jl") | |
# | |
# Another option is to define foo() inside of run() | |
# as local variables still seem to be accessible. | |
# | |
# This just makes for a neat single-file example. | |
for proc in procs | |
Distributed.@spawnat proc eval(foo_quote) | |
end | |
# Run functionality | |
futures = [] | |
names = ["Miles" for _ in 1:length(procs)] | |
for (proc, name) in zip(procs, names) | |
future = Distributed.@spawnat proc foo(name) | |
push!(futures, future) | |
end | |
# Get results | |
for future in futures | |
push!(results, Distributed.fetch(future)) | |
end | |
finally | |
Distributed.rmprocs(procs...) | |
end | |
return results | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment