Created
December 22, 2013 09:05
-
-
Save k14i/8080011 to your computer and use it in GitHub Desktop.
ElixirでのMessage Passing入門 ref: http://qiita.com/keithseahus/items/58e9cd53c908dcdb4e76
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)5> Node.connect :"baz@mba11-keith" | |
true | |
iex(foo@mba11-keith)6> Node.list | |
[:"bar@mba11-keith", :"baz@mba11-keith"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(bar@mba11-keith)6> Node.list | |
[:"foo@mba11-keith", :"baz@mba11-keith"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(baz@mba11-keith)1> Node.list | |
[:"foo@mba11-keith", :"bar@mba11-keith"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)7> func = fn -> IO.inspect Node.self end | |
#Function<20.80484245 in :erl_eval.expr/5> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)1> Node.self | |
:"foo@mba11-keith" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(qux@mba11-keith)6> target <- {:german, 'Welt'} | |
{:german, 'Welt'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(bar@mba11-keith)7> Client.start | |
{:register, #PID<0.82.0>} | |
tock in client | |
tock in client | |
tock in client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(bar@mba11-keith)1> Node.self | |
:"bar@mba11-keith" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(baz@mba11-keith)2> Client.start | |
{:register, #PID<0.75.0>} | |
tock in client | |
tock in client | |
tock in client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)2> Node.list | |
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(bar@mba11-keith)2> Node.list | |
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)3> Node.connect :"bar@mba11-keith" | |
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(bar@mba11-keith)3> Node.list | |
[:"foo@mba11-keith"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(foo@mba11-keith)4> Node.list | |
[:"bar@mba11-keith"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Hello do | |
def hello do | |
receive do | |
{:english, target} -> | |
IO.puts "Hello, #{target}." | |
{:japanese, target} -> | |
IO.puts "こんにちは、#{target}。" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Hello do | |
def hello do | |
receive do | |
{:english, target} -> | |
IO.puts "Hello, #{target}." | |
hello | |
{:japanese, target} -> | |
IO.puts "こんにちは、#{target}。" | |
hello | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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