Skip to content

Instantly share code, notes, and snippets.

@choallin
Created October 17, 2019 14: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 choallin/4e1d12d655bc536ac453d850b4acf562 to your computer and use it in GitHub Desktop.
Save choallin/4e1d12d655bc536ac453d850b4acf562 to your computer and use it in GitHub Desktop.
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