init
This commit is contained in:
commit
8293234a24
5 changed files with 107 additions and 0 deletions
36
lib/process_communication.ex
Normal file
36
lib/process_communication.ex
Normal file
|
@ -0,0 +1,36 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue