CodeFly: run two ruby methods in parallel (Parallel gem alternative)

CodeFly is a gem that I made, that allows you to do parallel / asynchronous programming in Rails.

This is how a classic code looks like:

@blue_cars = Vehicle.where(type: "car", color: "blue") # 1 sec
@accessories = Accessory.where(type: "car") # 1 sec
some_code(@blue_cars, @accessories) # 0.5 sec

This is how a flied code looks like:

fly(:A) { @blue_cars = Vehicle.where(type: "car", color: "blue") } # 1 sec
fly(:B) { @accessories = Accessory.where(type: "car") } # 1 sec
wait_fly(:A, :B)
some_code(@blue_cars, @accessories) # 0.5 sec

The first code will take 2.5 sec, but the second will take only 1.5, almost the twice of speed. On the second code, the A and B queries will be executed at the same time (I will assume that your DB have more than 1 core), and the Ruby will be free to execute your own code while these 2 queries are being processed.

The Fly methods are:

  • fly(id) { async code }
  • wait_fly(id_1[, id_2, …]): block until the async codes terminate

To use code fly, add it to your GemFile:

gem 'codefly'

then install with:

bundle install
Close Menu