-
Notifications
You must be signed in to change notification settings - Fork 55
/
gen_stage.exs
63 lines (49 loc) · 1.21 KB
/
gen_stage.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
Mix.install([
{:gen_stage, "~> 1.1"}
])
defmodule Producer do
use GenStage
def start_link(_args) do
GenStage.start_link(__MODULE__, [], name: Producer)
end
def init(_) do
{:producer, 0}
end
def handle_demand(demand, counter) do
# If the counter is 3 and we ask for 2 items, we will
# emit the items 3 and 4, and set the state to 5.
events = Enum.to_list(counter..(counter + demand - 1))
{:noreply, events, counter + demand}
end
end
defmodule Consumer do
use GenStage
def start_link(_args) do
GenStage.start_link(__MODULE__, [])
end
def init(_) do
{:consumer, :whatever, subscribe_to: [{Producer, min_demand: 0, max_demand: 5}]}
end
def handle_events(events, _from, state) do
# Wait for a second.
Process.sleep(1_000)
# Inspect the events.
IO.inspect(events)
# We are a consumer, so we would never emit items.
{:noreply, [], state}
end
end
defmodule Main do
def main do
children = [
Producer,
Consumer
]
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
end
end
Main.main()
# Unless running from IEx, sleep indefinitely so stages keep running
unless IEx.started?() do
Process.sleep(:infinity)
end