jonasbhjulstad

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

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

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!

Last Post!

garrison

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"

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement