Created
December 14, 2016 07:35
-
-
Save marionzualo/300b04f9b21a47ae673324b8667c001f to your computer and use it in GitHub Desktop.
Fibers fourth example
This file contains 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 "fiber" | |
def find_in_batches_with_fiber | |
Fiber.new do | |
index = 0 | |
chunk_size = 2 | |
while batch = query_batch(index, chunk_size) | |
break if batch.length == 0 | |
Fiber.yield batch | |
index += 1 | |
end | |
end | |
end | |
def complex_operation_in_background_with_fibers(fiber = nil) | |
fiber ||= find_in_batches_with_fiber | |
if fiber.alive? && (batch = fiber.resume) | |
process_batch(batch) | |
complex_operation_in_background_with_fibers(fiber) | |
end | |
end | |
# Execution in the terminal | |
# >> complex_operation_in_background_with_fibers | |
# ["Vhils", "AKACORLEONE"] | |
# ["Pixel Pancho", "+-"] | |
# ["Kruella D'Enfer", "Tamara Alves"] | |
# ["Add Fuel", "MAR"] | |
# ["Eime", "Bordalo II"] | |
# ["Felipe Pantone", "Ernest Zacharevic"] | |
# ["Wasted Rita", "Sainer"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment