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 https://github.com/phoenixframework/phoenix_pubsub/blob/master/test/phoenix/distributed_pubsub_test.exs

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!

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement