🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Distributed at a First Look

Distributed computing in Julia means running work in separate processes that Julia manages as workers. Each worker is its own Julia process, and the main process asks workers to run work.

Two workers can be added

This script adds two workers and prints how many workers exist.

using Distributed
addprocs(2)
println(nworkers())

The worker list holds two workers

This script adds two workers and prints how many entries the worker list holds.

using Distributed
addprocs(2)
println(length(workers()))

Two workers is the expected count

This script adds two workers and prints whether the worker count equals two.

using Distributed
addprocs(2)
println(nworkers() == 2)

Example

using Distributed
addprocs(2)
println(nworkers())
println(length(workers()))

Key points

  • Worker: a separate Julia process that the main process can use for work.
  • Distributed tools: using Distributed brings worker tools into a script.
  • Use addprocs(2) to add two workers to a script.
  • Use nworkers() to count the workers added to a script.
  • Fresh start: each script starts with no added workers, so each script adds its own workers.

In this example, the script adds two workers, then it prints the worker count and then it prints the length of the worker list.

Note

workers are separate processes, not threads, so they do not share memory with the main process.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

What does nworkers() report after addprocs(2)?