在Ruby中一次读取文件N行 [英] Reading a file N lines at a time in ruby
本文介绍了在Ruby中一次读取文件N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大文件(数百兆),其中包含文件名,每行一个.
I have a large file (hundreds of megs) that consists of filenames, one per line.
我需要遍历文件名列表,并为每个文件名创建一个进程.我一次最多要8个分支的进程,并且我不想一次将整个文件名列表读入RAM.
I need to loop through the list of filenames, and fork off a process for each filename. I want a maximum of 8 forked processes at a time and I don't want to read the whole filename list into RAM at once.
我什至不知道从哪里开始,有人可以帮我吗?
I'm not even sure where to begin, can anyone help me out?
推荐答案
File.foreach("large_file").each_slice(8) do |eight_lines|
# eight_lines is an array containing 8 lines.
# at this point you can iterate over these filenames
# and spawn off your processes/threads
end
这篇关于在Ruby中一次读取文件N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文