Ich versuche zu verstehen, was in diesem Stück Elixir-Code vor sich geht. Wir haben zwei Prozesse hervorgebracht, und dann erzeugt der erzeugte Prozess die Nachricht zurück zum Absender, aber ich sehe nur eine Nachricht in iex. Was ist los mit dir?Warum wird die erzeugte Prozessnachricht vom Absender nicht erneut empfangen?
defmodule TwoPs do
# a simple echo function - take a pid to send message back to
def echo(pid) do
#wait till the spawned process receives a message, turn around and echo it back to the sender
receive do
msg ->
IO.puts("Received #{msg}")
send pid, {:ok, "Hello #{msg}"}
end
end
#spawn's two processes and sends a message to both...expects an echo back from both spawned processes
def create_ps() do
#spawn the two processes
p1 = spawn(TwoPs, :echo, [self])
p2 = spawn(TwoPs, :echo, [self])
#send a message to the first pid
send p1, "World"
#receive message back from spawned child 1
receive do
{:ok, msg} -> "#{msg}"
end
#send a message to the second pid
send p2, "Bob"
#receive message from the spawned child 2
receive do
{:ok, msg} -> "#{msg}"
end
end
end
Der Ausgang in iex ist immer "Hallo Bob"
iex(17)> TwoPs.create_ps
Received World
Received Bob
"Hello Bob"
Warum sehen "Hallo Welt" nicht wahr?
Danke. Erstaunlich schnelle Antwort. Das komplett verpasst. Dummer Fehler meinerseits – sat