spawn
creates a new Erlang VM process to run a given module, function, and arguments and returns its process id pid
.
defmodule Bob do
def say(message) do
IO.puts "[#{inspect self}] Bob says: #{message}"
end
end
iex> Bob.say "I'm Bob"
[#PID<0.57.0>] Bob says: sup
:ok
iex> spawn(Bob, :say, ["I'm Bob"])
[#PID<0.101.0>] Bob says: I'm Bob
#PID<0.101.0>
self
returns the current process’pid
Check the PID
s that are printed. We’ve just run the Bob.say
function as a separate process.
Additional reading:
- Processes
iex> h Process