Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 流中的并行处理不是并行的吗?_Elixir - Fatal编程技术网

Elixir 流中的并行处理不是并行的吗?

Elixir 流中的并行处理不是并行的吗?,elixir,Elixir,我有一个简单的模块 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

我有一个简单的模块

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
现在,当运行Flow.F1.t1()时,我得到以下结果:

Processing one
Processed one
Processing two
Processed two
Processing three
Processed three
["TWO", "ONE", "THREE"]
处理过程需要6秒钟,因为它在每个P1调用中等待2秒,但我(从文档中)预计,
Elixir.map
将并行处理项目。所以我们应该看到这样的情况:

Processing one
Processing two
Processing three
Processed one
Processed two
Processed three

只需2秒。有人能解释一下我在这里遗漏了什么吗?

流程处理500个项目的批次,如中所述

我认为您的输入数据不足以将其拆分为多个消费者,因此只有一个消费者承担所有工作。
如果您将选项
max\u demand:1
添加到您的
流中。从_enumerable
它将并行消耗。

谢谢,它真的完成了任务。在本例中可以省略Flow.partition。