Skip to content

Instantly share code, notes, and snippets.

@kshahkshah
Last active October 11, 2016 20:39
Show Gist options
  • Save kshahkshah/2b21d9f7e15c143537a3248b3ae3097c to your computer and use it in GitHub Desktop.
Save kshahkshah/2b21d9f7e15c143537a3248b3ae3097c to your computer and use it in GitHub Desktop.
class Roo::Base
def each_sheet_concurrently(&block)
jobs = Concurrent::Array.new
self.sheets.each do |name|
jobs << Concurrent.future {
block.call(self.sheet(name))
}
end
Concurrent.zip(*jobs).value
end
def each_row_concurrently(&block)
jobs = Concurrent::Array.new
name = @default_sheet
1.upto(sheet(name).last_row) do |line|
jobs << Concurrent.future {
block.call(sheet(name).row(line, name))
}
end
Concurrent.zip(*jobs).value
end
def each_concurrently(&block)
jobs = Concurrent::Array.new
self.sheets.each do |name|
1.upto(sheet(name).last_row) do |line|
jobs << Concurrent.future {
block.call(sheet(name).row(line, name), name)
}
end
end
Concurrent.zip(*jobs).value
end
end
# file_path = some big excel file with 6 sub-sheets / tabs. One sheet is much bigger than the others...
roo = Roo::Spreadsheet.open(file_path)
rows_per_sheet = Concurrent::Array.new
all_rows = Concurrent::Array.new
foo = roo.each_sheet_concurrently do |sheet|
# do some work at the sheet level for whatever reason
rows_per_sheet << sheet.last_row
# and process rows...
sheet.each_row_concurrently do |row|
all_rows << row
end
end
# binding.pry
# all_rows here will not be filled up yet...
# use the other method I've defined...
spread = Concurrent::Hash.new
roo.each_concurrently do |row, sheet|
spread[sheet] ||= Concurrent::Array.new
spread[sheet] << row
end
# and this will work perfectly fine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment