process-communication/lib/process_communication.ex
2023-04-17 11:43:58 +03:00

37 lines
683 B
Elixir

defmodule Child do
def start_link do
GenServer.start_link(__MODULE__, [])
end
def init(_args) do
{:ok, 0}
end
def handle_info({:info, n}, state) do
IO.puts "info #{n}"
{:noreply, state + n}
end
def handle_cast({:cast, n}, state) do
IO.puts "cast #{n}"
{:noreply, state + n}
end
def handle_call({:call, n}, _from, state) do
IO.puts "call #{n}"
{:reply, state + n, state + n}
end
end
defmodule ProcessCommunication do
def run do
{:ok, child} = Child.start_link()
send(child, {:info, 7})
GenServer.cast(child, {:cast, 17})
result = GenServer.call(child, {:call, 31})
IO.puts "Rusult: #{result}"
end
end