Ruby 的 CSV.open 是否缓冲到内存并一次写入?
Does Ruby's CSV.open buffer to memory and write all at once?
是CSV.open
在block退出的时候把数据存入内存写入文件一次,还是自动分批写入?
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row
end
end
CSV.open
将在块关闭时写入底层 OS,并且每次缓冲区填充和刷新时它也会写入,这将自动发生。 (在我的 Ruby 安装中,它发生在 8196 字节处。)您还可以将 csv.flush
添加到您的块以强制它按顺序写入。
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row # Without flush, writes to underlying OS only when buffer fills
csv.flush # Adding this forces a write to underlying OS
end
end # Exiting block writes to underlying OS and closes file handle
(请注意,写入是对底层 OS,这可能会在实际写入磁盘之前再次缓冲流。)
是CSV.open
在block退出的时候把数据存入内存写入文件一次,还是自动分批写入?
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row
end
end
CSV.open
将在块关闭时写入底层 OS,并且每次缓冲区填充和刷新时它也会写入,这将自动发生。 (在我的 Ruby 安装中,它发生在 8196 字节处。)您还可以将 csv.flush
添加到您的块以强制它按顺序写入。
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row # Without flush, writes to underlying OS only when buffer fills
csv.flush # Adding this forces a write to underlying OS
end
end # Exiting block writes to underlying OS and closes file handle
(请注意,写入是对底层 OS,这可能会在实际写入磁盘之前再次缓冲流。)