I explored the possibilities of ruby ββparallel / asynchronous processing and read many articles and blog posts. I looked through EventMachine, Fibers, Revactor, Reia, etc. Etc. Unfortunately, I could not find a simple, effective (and non-IO-blocking) solution for this very simple use case:
File.open('somelogfile.txt') do |file| while line = file.gets # (R) Read from IO line = process_line(line) # (P) Process the line write_to_db(line) # (W) Write the output to some IO (DB or file) end end
You can see my little script does three reads ( R ), process ( P ) and writes ( W ). Suppose, for simplicity, that each operation takes exactly 1 unit of time (for example, 10 ms), so the current code will do something like this (5 lines):
Time: 123456789012345 (15 units in total) Operations: RPWRPWRPWRPWRPW
But I would like him to do something like this:
Time: 1234567 (7 units in total) Operations: RRRRR PPPPP WWWWW
Obviously, I could start three processes (reader, processor, and writer) and transfer the read lines from the reader to the processor queue, and then transfer the processed lines to the write queue (all are coordinated, for example, using RabbitMQ). But the precedent is so simple that it just does not feel good.
Any tips on how this can be done (without switching from Ruby to Erlang, Closure or Scala)?
ruby asynchronous concurrency fiber eventmachine
Dim
source share