在Elixir中附加到文件的最佳方法是什么?

nat*_*ess 5 elixir

我一直坚持一些简单的任务.我们假设我们有一些伪代码:

Enum.each 1..1_000_000, fn(id) ->
    some_complex_method(id) |> save_results
end
Run Code Online (Sandbox Code Playgroud)

哪里save_results

def save_results(data) do
    {:ok, file} = File.open "data.log", [:append]
    Enum.each(data, &(IO.binwrite(file, &1)))
    File.close file
end
Run Code Online (Sandbox Code Playgroud)

问题是,如果我们的范围非常大,我们会花时间打开和关闭文件.工作完成后如何处理打开状态和调用close方法?

Ono*_*cci 0

上面的代码看起来似乎不会在每次写入时打开和关闭文件,但如果有一些东西你没有显示,那么我建议你也许想File.open/2File.stream!/3