Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

elixir - Parallel processing in Flow is not parallel?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Flow work with batches of 500 items, as stated in the docs.

I think your input data is not enough to flow splits it into more than one consumer, so only one consumer is taking all the work. If you add the option max_demand: 1 to your Flow.from_enumerable it will consume in parallel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...