Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a file N lines at a time in ruby

Tags:

fork

file-io

ruby

I have a large file (hundreds of megs) that consists of filenames, one per line.

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?

like image 337
Sam Avatar asked Mar 07 '26 23:03

Sam


1 Answers

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
like image 95
glenn jackman Avatar answered Mar 09 '26 12:03

glenn jackman