Created
October 13, 2016 18:20
-
-
Save kshahkshah/7d02c55aa61a1c7a1ea174de155cb018 to your computer and use it in GitHub Desktop.
Roo && concurrent-ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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