Skip to content

Instantly share code, notes, and snippets.

@kshahkshah
Created October 13, 2016 18:20
Show Gist options
  • Save kshahkshah/7d02c55aa61a1c7a1ea174de155cb018 to your computer and use it in GitHub Desktop.
Save kshahkshah/7d02c55aa61a1c7a1ea174de155cb018 to your computer and use it in GitHub Desktop.
Roo && concurrent-ruby
require 'concurrent'
require 'concurrent-edge'
# require 'benchmark'
# require 'pry'
# rails...
#require './config/environment'
class Roo::Base
def each_sheet_concurrently(&block)
jobs = self.sheets.map do |name|
Concurrent.future do
block.call(self.sheet(name), name)
end
end
Concurrent.zip(*jobs).value
end
def each_row_concurrently(name, &block)
jobs = 1.upto(last_row(name)).map do |line|
Concurrent.future do
block.call(row(line, name))
end
end
Concurrent.zip(*jobs).value
end
# alternate method...
def each_concurrently(&block)
jobs = Concurrent::Array.new
self.sheets.each do |name|
1.upto(sheet(name).last_row) do |line|
jobs << Concurrent.future do
block.call(sheet(name).row(line, name), name)
end
end
end
Concurrent.zip(*jobs).value
end
end
# file_path = some_excelx_spreadsheet...
spreadsheet = Roo::Spreadsheet.open(file_path)
rows_per_sheet = Array.new
all_rows = Array.new
spreadsheet.each_sheet_concurrently do |sheet, name|
# some operation on the sheet
rows_per_sheet << sheet.last_row
spreadsheet.each_row_concurrently(name) do |row|
# some operation on rows
all_rows << row
end
end
puts all_rows.count
# spread = Concurrent::Hash.new
# spreadsheet.each_concurrently do |row, sheet|
# spread[sheet] ||= Concurrent::Array.new
# spread[sheet] << row
# end
# binding.pry
# so we don't advance and not hit the debugger...
# foo = "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment