Created
December 14, 2016 07:51
-
-
Save marionzualo/cd0cf25d94ef13d634eb2abeea609aa7 to your computer and use it in GitHub Desktop.
All fibers
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
######################## | |
# First example | |
def find_in_batches | |
index = 0 | |
chunk_size = 2 | |
while batch = query_batch(index, chunk_size) | |
return if batch.length == 0 | |
yield batch | |
index += 1 | |
end | |
end | |
def query_batch(index, chunk_size) | |
range_beginning = index * chunk_size | |
large_dataset[range_beginning...(range_beginning+2)] | |
end | |
def large_dataset | |
[ | |
"Vhils", | |
"AKACORLEONE", | |
"Pixel Pancho", | |
"+-", | |
"Kruella D'Enfer", | |
"Tamara Alves", | |
"Add Fuel", | |
"MAR", | |
"Eime", | |
"Bordalo II", | |
"Felipe Pantone", | |
"Ernest Zacharevic", | |
"Wasted Rita", | |
"Sainer" | |
] | |
end | |
######################## | |
# Second example | |
def process_batch(batch) | |
p batch | |
end | |
# this method runs in a background job | |
def complex_operation_in_background | |
find_in_batches do |batch| | |
# the complex operation on the given batch | |
process_batch(batch) | |
end | |
end | |
# Execution in the terminal | |
# >> complex_operation_in_background | |
# ["Vhils", "AKACORLEONE"] | |
# ["Pixel Pancho", "+-"] | |
# ["Kruella D'Enfer", "Tamara Alves"] | |
# ["Add Fuel", "MAR"] | |
# ["Eime", "Bordalo II"] | |
# ["Felipe Pantone", "Ernest Zacharevic"] | |
# ["Wasted Rita", "Sainer"] | |
######################## | |
# Third example | |
def complex_operation_in_background_with_recursion(batch_index = 0) | |
chunk_size = 2 | |
batch = query_batch(batch_index, chunk_size) | |
return if batch.length == 0 | |
process_batch(batch) | |
complex_operation_in_background_with_recursion(batch_index + 1) | |
end | |
# Execution in the terminal | |
# >> complex_operation_in_background_with_recursion | |
# ["Vhils", "AKACORLEONE"] | |
# ["Pixel Pancho", "+-"] | |
# ["Kruella D'Enfer", "Tamara Alves"] | |
# ["Add Fuel", "MAR"] | |
# ["Eime", "Bordalo II"] | |
# ["Felipe Pantone", "Ernest Zacharevic"] | |
# ["Wasted Rita", "Sainer"] | |
######################## | |
# 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