-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking-with-multiple-processes-2.exs
67 lines (58 loc) · 1.52 KB
/
working-with-multiple-processes-2.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule WorkingWithMultipleProcesses do
require IEx
# Exercise:WorkingWithMultipleProcesses-2
# Write a program that spawns two processes and then passes each
# a unique token (for example "fred", "betty"). Have them send
# the tokens back
#
# - Is the order in which the replies are received deterministic in theory?
# In practice?
# - If either answer is no, how could you make it so?
def listen do
receive do
{sender, msg} ->
send sender, {:ok, msg, self()}
listen()
end
end
def run do
pids = for p <- 1..2, into: [], do: spawn(WorkingWithMultipleProcesses, :listen, [])
pids
|> Enum.zip(["fred", "betty"])
|> Stream.cycle()
|> Enum.take(10)
|> Stream.each(&IO.inspect/1)
|> Enum.each(&send_msg/1)
main_listen()
end
def send_msg({pid, msg}), do: send pid, {self(), msg}
def main_listen do
receive do
{:ok, msg, sender} ->
IO.puts "I've received #{msg}"
main_listen()
end
end
end
WorkingWithMultipleProcesses.run()
# =>
# {#PID<0.113.0>, "foo"}
# {#PID<0.114.0>, "betty"}
# {#PID<0.113.0>, "foo"}
# {#PID<0.114.0>, "betty"}
# {#PID<0.113.0>, "foo"}
# {#PID<0.114.0>, "betty"}
# {#PID<0.113.0>, "foo"}
# {#PID<0.114.0>, "betty"}
# {#PID<0.113.0>, "foo"}
# {#PID<0.114.0>, "betty"}
# I've received foo
# I've received betty
# I've received foo
# I've received betty
# I've received foo
# I've received betty
# I've received foo
# I've received betty !! \ Not in order
# I've received betty !! /
# I've received foo