Created
October 17, 2019 14:35
-
-
Save choallin/4e1d12d655bc536ac453d850b4acf562 to your computer and use it in GitHub Desktop.
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
def process_partial_file_input(data) | |
# Do something cool with data | |
end | |
# Event loop | |
big_file = "network/path/big_file.xlsx" | |
file_handler = open(big_file) | |
files_to_look_after = [file_handler] | |
loop do | |
puts "(Re)Starting the Event loop" | |
# 1. Step: Check if IO event is ready from the OS. Wait | |
# for 10 ms to be notified | |
# A more advanced event loop wouldn't use select since this is | |
# activly asking the os but would use something like epoll. | |
# But this is not available in Ruby so we fallback to this | |
# method | |
readable, _writeable = IO.select files_to_look_after, [], [], 0.01 | |
# 2. Step: Handle readable connection | |
if readable | |
readable.each do |ready_io| | |
# We will read 4096 bytes in each iteration | |
read_data = ready_io.read_nonblock(4096) | |
# 3. Step: Process data according to business logic | |
process_partial_file_input read_data if read_data | |
rescue EOFError => e | |
files_to_look_after.reject! {|file| file == ready_io } | |
end | |
end | |
break if files_to_look_after.empty? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment