jonasbhjulstad
Adding code to child nodes
Hi!
I have been able to figure out how to spawn child nodes, but I’m not able to make the child nodes do anything. This is my current code:
:os.cmd('epmd -daemon')
{:ok, parent} = Node.start(:parent@localhost, :shortnames)
:child.start(:localhost, 'child_1')
Spawning a function in the child node results in an error:
Node.spawn(:child_1@localhost, fn -> IO.puts "test" end)
IO.puts appears as undefined for the child node. How do i load code into child nodes?
Marked As Solved
benwilson512
A good example might be to checkout what Phoenix does in its pubsub test suite which includes some multi node cases phoenix_pubsub/test/phoenix/distributed_pubsub_test.exs at main · phoenixframework/phoenix_pubsub · GitHub
Also Liked
garrison
With :peer, which replaced the now-deprecated :slave:
opts = %{name: ~c"foo", host: ~c"localhost", connection: :standard_io}
{:ok, peer_pid, _name} = :peer.start_link(opts)
:peer.call(peer_pid, :code, :add_paths, [:code.get_path()])
:peer.call(peer_pid, IO, :puts, ["test"])
# "test"
hauleth
Problem there is that slave node was spawned without any extra libraries outside the Erlang standard distribution. That mean that module IO (Elixir.IO to be exact) do not exists on that module. With save module started like this you can use only Erlang standard library, so to achieve what you want you need to use fn -> :io.format("test") end.
jonasbhjulstad
Thanks for the help!
I had a look into some of the code, and was able to find the correct commands which let me load the code path into the slave:
:os.cmd('epmd -daemon')
Node.start(:master@localhost, :shortnames)
:slave.start(:localhost, 'slave')
:rpc.block_call(:test@localhost, :code, :add_paths, [:code.get_path()])
...
Node.spawn(:slave@localhost, fn -> IO.puts "test" end)
...
#PID<10741.85.0>
iex(master@localhost)9> test
It worked!








