I have a following simple module.
defmodule Flow.F1 do
def t1 do
["one", "two", "three"]
|> Flow.from_enumerable()
|> Flow.map(&p1(&1))
|> Flow.partition()
|> Enum.to_list()
end
defp p1(item) do
IO.puts("Processing #{item}")
:timer.sleep(2000)
IO.puts("Processed #{item}")
String.upcase(item)
end
end
Now when running Flow.F1.t1() I'm getting this result:
Processing one
Processed one
Processing two
Processed two
Processing three
Processed three
["TWO", "ONE", "THREE"]
Processing takes 6 seconds as it waits 2sec in each P1 call, but I would expect (from documentation) that Elixir.map
is processing items in parallel. So we should see something like:
Processing one
Processing two
Processing three
Processed one
Processed two
Processed three
And should take 2sec only. Can someone explain what I'm missing here?
question from:
https://stackoverflow.com/questions/65829241/parallel-processing-in-flow-is-not-parallel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…