Skip to content

Instantly share code, notes, and snippets.

@k14i
Created December 22, 2013 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k14i/8080011 to your computer and use it in GitHub Desktop.
Save k14i/8080011 to your computer and use it in GitHub Desktop.
ElixirでのMessage Passing入門 ref: http://qiita.com/keithseahus/items/58e9cd53c908dcdb4e76
% iex --sname foo --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(foo@mba11-keith)1>
% iex --sname bar --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(bar@mba11-keith)1>
iex(bar@mba11-keith)4>
=ERROR REPORT==== 22-Dec-2013::14:51:30 ===
** Connection attempt from disallowed node 'baz@mba11-keith' **
nil
iex(bar@mba11-keith)5> Node.list
[:"foo@mba11-keith"]
% iex --sname baz --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(baz@mba11-keith)1>
iex(foo@mba11-keith)5> Node.connect :"baz@mba11-keith"
true
iex(foo@mba11-keith)6> Node.list
[:"bar@mba11-keith", :"baz@mba11-keith"]
iex(bar@mba11-keith)6> Node.list
[:"foo@mba11-keith", :"baz@mba11-keith"]
iex(baz@mba11-keith)1> Node.list
[:"foo@mba11-keith", :"bar@mba11-keith"]
iex(foo@mba11-keith)7> func = fn -> IO.inspect Node.self end
#Function<20.80484245 in :erl_eval.expr/5>
iex(foo@mba11-keith)8> Node.spawn(:"foo@mba11-keith", func)
#PID<0.65.0>
:"foo@mba11-keith"
iex(foo@mba11-keith)9> Node.spawn(:"bar@mba11-keith", func)
:"bar@mba11-keith"
#PID<8620.66.0>
iex(foo@mba11-keith)10> Node.spawn(:"baz@mba11-keith", func)
#PID<8692.59.0>
:"baz@mba11-keith"
% iex --sname qux --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(qux@mba11-keith)1> c("hello0.ex")
[Hello]
iex(qux@mba11-keith)2> target = spawn(Hello, :hello, [])
#PID<0.55.0>
iex(qux@mba11-keith)3> target <- {:english, 'World'}
Hello, World.
{:english, 'World'}
iex(qux@mba11-keith)4> target <- {:japanese, '世界'}
{:japanese, [19990, 30028]}
iex(qux@mba11-keith)5> target <- {:english, 'World'}
{:english, 'World'}
iex(foo@mba11-keith)1> Node.self
:"foo@mba11-keith"
% iex --sname qux --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(qux@mba11-keith)1> c("hello1.ex")
hello1.ex:1: redefining module Hello
[Hello]
iex(qux@mba11-keith)2> target = spawn(Hello, :hello, [])
#PID<0.55.0>
iex(qux@mba11-keith)3> target <- {:english, 'World'}
Hello, World.
{:english, 'World'}
iex(qux@mba11-keith)4> target <- {:japanese, '世界'}
こんにちは、世界。
{:japanese, [19990, 30028]}
iex(qux@mba11-keith)5> target <- {:english, 'World'}
{:english, 'World'}
Hello, World.
iex(qux@mba11-keith)6> target <- {:german, 'Welt'}
{:german, 'Welt'}
% iex --sname qux --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(qux@mba11-keith)1> c("hello2.ex")
hello2.ex:1: redefining module Hello
[Hello]
iex(qux@mba11-keith)2> target = spawn(Hello, :hello, [])
#PID<0.55.0>
iex(qux@mba11-keith)3> target <- {:japanese, '世界'}
こんにちは、世界。
{:japanese, [19990, 30028]}
iex(qux@mba11-keith)4> target <- {:english, 'World'}
Hello, World.
{:english, 'World'}
iex(qux@mba11-keith)5> target <- {:german, 'Welt'}
{:german, 'Welt'}
Huh?
% iex --sname qux --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(qux@mba11-keith)1> c("hello3.ex")
hello3.ex:1: redefining module Hello
[Hello]
iex(qux@mba11-keith)2> target = spawn(Hello, :hello, [])
#PID<0.55.0>
iex(qux@mba11-keith)3> target <- :exit
:exit
Bye!
iex(qux@mba11-keith)4> target <- {:english, 'World'}
{:english, 'World'}
% iex --sname qux --cookie elixir-example
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(qux@mba11-keith)1> c("ticker.ex")
[Client, Tick]
iex(qux@mba11-keith)2> Node.connect :"foo@mba11-keith"
true
iex(qux@mba11-keith)3> Node.connect :"bar@mba11-keith"
true
iex(qux@mba11-keith)4> Node.connect :"baz@mba11-keith"
true
iex(qux@mba11-keith)5> Tick.start
:yes
tick
tick
tick
tick
tick
iex(qux@mba11-keith)6> Client.start
registering #PID<0.76.0>
{:register, #PID<0.76.0>}
tick
tock in client
tick
tock in client
tick
tock in client
tick
tock in client
iex(foo@mba11-keith)11> c("../nodes/ticker.ex")
[Client, Tick]
iex(foo@mba11-keith)12> Client.start
{:register, #PID<0.86.0>}
tock in client
tock in client
tock in client
iex(bar@mba11-keith)7> Client.start
{:register, #PID<0.82.0>}
tock in client
tock in client
tock in client
iex(bar@mba11-keith)1> Node.self
:"bar@mba11-keith"
iex(baz@mba11-keith)2> Client.start
{:register, #PID<0.75.0>}
tock in client
tock in client
tock in client
iex(foo@mba11-keith)2> Node.list
[]
iex(bar@mba11-keith)2> Node.list
[]
iex(foo@mba11-keith)3> Node.connect :"bar@mba11-keith"
true
iex(bar@mba11-keith)3> Node.list
[:"foo@mba11-keith"]
iex(foo@mba11-keith)4> Node.list
[:"bar@mba11-keith"]
% iex --sname baz --cookie ELIXIR-EXAMPLE
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.11.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(baz@mba11-keith)1> Node.connect :"bar@mba11-keith"
false
defmodule Hello do
def hello do
receive do
{:english, target} ->
IO.puts "Hello, #{target}."
{:japanese, target} ->
IO.puts "こんにちは、#{target}。"
end
end
end
defmodule Hello do
def hello do
receive do
{:english, target} ->
IO.puts "Hello, #{target}."
hello
{:japanese, target} ->
IO.puts "こんにちは、#{target}。"
hello
end
end
end
defmodule Hello do
def hello do
receive do
{:english, target} ->
IO.puts "Hello, #{target}."
hello
{:japanese, target} ->
IO.puts "こんにちは、#{target}。"
hello
_ ->
IO.puts "Huh?"
hello
end
end
end
defmodule Hello do
def hello do
receive do
{:english, target} ->
IO.puts "Hello, #{target}."
hello
{:japanese, target} ->
IO.puts "こんにちは、#{target}。"
hello
:exit ->
IO.puts "Bye!"
_ ->
IO.puts "Huh?"
hello
end
end
end
defmodule Tick do
@interval 2000
@name :ticker
def start do
pid = spawn(__MODULE__, :generator, [[]])
:global.register_name(@name, pid)
end
def register(client_pid) do
:global.whereis_name(@name) <- {:register, client_pid}
end
def generator(clients) do
receive do
{:register, pid} ->
IO.puts "registering #{inspect pid}"
generator([pid|clients])
after
@interval ->
IO.puts "tick"
Enum.each clients, fn client -> client <- {:tick} end
generator(clients)
end
end
end
defmodule Client do
def start do
pid = spawn(__MODULE__, :receiver, [])
Tick.register(pid)
end
def receiver do
receive do
{:tick} ->
IO.puts "tock in client"
receiver
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment