Skip to content

Instantly share code, notes, and snippets.

@marionzualo
Created December 14, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marionzualo/300b04f9b21a47ae673324b8667c001f to your computer and use it in GitHub Desktop.
Save marionzualo/300b04f9b21a47ae673324b8667c001f to your computer and use it in GitHub Desktop.
Fibers fourth example
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