Ruby的CSV.open缓冲到内存并一次写入所有内容吗?

eee*_*eee 5 ruby csv large-files

CSV.open存储的数据在内存中,并写入文件一次当块退出,或者它会自动在多批写?

require 'csv'

CSV.open('result.csv', 'wb') do |csv|
  while row = next_row
    csv << row
  end
end
Run Code Online (Sandbox Code Playgroud)

use*_*951 8

CSV.open当块关闭时,它将写入底层操作系统,并且每次缓冲区填满并刷新时它也会写入,这将自动发生.(在我的Ruby安装中,它发生在8196字节.)您还可以添加csv.flush到块中以强制它按顺序写入.

require 'csv'

CSV.open('result.csv', 'wb') do |csv|
  while row = next_row
    csv << row  # Writes to underlying OS only when buffer fills
    csv.flush   # Forces write to underlying OS
  end
end             # Writes to underlying OS and closes file handle
Run Code Online (Sandbox Code Playgroud)

(请注意,写入操作是基础操作系统,它可能会在实际写入磁盘之前再次缓冲流.)