This document provides an overview of Experimental.Flow, an Elixir module that allows processing of collections in parallel using flow-based programming. It discusses how Flow builds on GenStage to allow concurrent processing of data across multiple cores. The document presents examples comparing eager, lazy, and concurrent approaches to word counting on a file. It demonstrates how Flow partitions data across stages using windows and dispatchers to process data in parallel.
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
Experimental.flow
1. Experimental.Flow
An overview of experimental Elixir Flow module that allows developers
to express processing steps for collections, like Stream, but utilizing the
power of parallel execution.
2. Resources
ElixirConf 2016 - Keynote by José Valim
www.youtube.com/watch?v=srtMWzyqdp8
Kyiv Elixir Meetup 3 - Flow-Based Programming with Elixir, Anton
Mishchuk
www.youtube.com/watch?v=yDaPaCxAVq8
www.slideshare.net/AntonMishchuk/flowbased-programming-with-
elixir
7. Helper functions
defp words_to_map(word, map) do
word
|> String.replace(~r/W/u, "")
|> filter_map(map)
end
defp filter_map("", map), do: map
defp filter_map(word, map) do
word = String.downcase(word)
Map.update(map, word, 1, &(&1 + 1))
end
8. Eager (Enum)
def process_eager(path_to_file) do
path_to_file
|> File.read!()
"The Project Gutenberg EBook of The Complete Works of William
Shakespeare, byrnWilliam Shakespearernrn …”
11. Lazy (Stream)
+ Allows us to “control” memory consumption
- Processing overhead
Allows us to work with large datasets without loading them all into
memory
15. Lazy (Stream)
"The Project Gutenberg EBook of The Complete Works
of William Shakespeare, byn"
"The", "Project", "Gutenberg", "EBook", "of", "The",
"Complete", "Works", "of", "William", "Shakespeare",
"by"
|> Enum.reduce(%{}, &words_to_map/2)
17. Concurrent (Flow)
+ Concurrency
+ Allows us to “control” memory consumption
- Processing overhead
- Processing order
Allows us to process large or infinite collections concurrently (on
multicore machines)
18. GenStage is a new Elixir behaviour for exchanging events
with back-pressure between Elixir processes
producer
producer
consumer
producer
consumer
consumer
26. Concurrent (Flow)
def process_flow(path_to_file) do
path_to_file
|> File.stream!()
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
end
P
27. Concurrent (Flow)
def process_flow(path_to_file) do
path_to_file
|> File.stream!()
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
end
P
PC PC
DemandDispatcher
28. Concurrent (Flow)
def process_flow(path_to_file) do
path_to_file
|> File.stream!()
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
end
P
PC PC
DemandDispatcher
PC PC
PartitionDispatcher
29. Concurrent (Flow)
def process_flow(path_to_file) do
path_to_file
|> File.stream!()
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
end
P
PC PC
DemandDispatcher
PartitionDispatcher
PC PC
C CReducers
%{} %{}
30. Concurrent (Flow)
P
PC PC
PC PC
C C
%{} %{}
"The Project Gutenberg EBook of
The Complete Works of William
Shakespeare, byn"
"William Shakespearen"
"The", "Project", "Gutenberg",
"EBook", "of", "The", "Complete",
"Works", "of", "William",
"Shakespeare,", "by"
"William", "Shakespeare"
"the", "project", "of", “the",
"william", "of", "by ", "william"
"gutenberg", "ebook",
"complete", "shakespeare",
"works", "shakespeare"
31. Concurrent (Flow)
def process_flow(path_to_file) do
path_to_file
|> File.stream!()
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
end
32. Concurrent (Flow): multiple sources
streams =
for file <- File.ls!(path_to_dir) do
File.stream!(path_to_dir <> "/" <> file,
read_ahead: 100_000)
end
34. Concurrent (Flow): multiple sources
streams
|> Flow.from_enumerables()
|> Flow.flat_map(&String.split/1)
|> Flow.map(&String.replace(&1, ~r/W/u, ""))
|> Flow.filter_map(fn w -> w != "" end, &String.downcase/1)
|> Flow.partition()
|> Flow.reduce(fn -> %{} end, fn word, map ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.into(%{})
FS
P P
FSFS
P
C
%{}
C
%{}
2 cores
3 files
35. Configuration (demand, the number of stages)
Flow.partition(stages: 8)
• :stages - the number of partitions (reducer stages)
• :hash - the hashing function
• :max_demand - the maximum demand for this subscription
• :min_demand - the minimum demand for this subscription
• …
36. Experimental.Flow.Window
Splits a flow into windows that are materialized at certain triggers.
window = Flow.Window.global
|> Flow.Window.trigger_every(10, :keep)
window = Flow.Window.global
|> Flow.Window.trigger_every(10, :reset)