Parallelizing methods in Rails

后端 未结 4 679
盖世英雄少女心
盖世英雄少女心 2021-02-07 06:42

My Rails web app has dozens of methods from making calls to an API and processing query result. These methods have the following structure:

def method_one
  batc         


        
4条回答
  •  既然无缘
    2021-02-07 07:28

    Ruby has the excellent promise gem. Your example would look like:

    require 'future'
    
    def method_one
    ...
    def method_nth
    
    def summary
      result1 = future { method_one }
      ......
      resultn = future { method_nth }
      collect_results result1, ..., resultn
    end
    

    Simple, isn't it? But let's get to more details. This is a future object:

    result1 = future { method_one }
    

    It means, the result1 is getting evaluated in the background. You can pass it around to other methods. But result1 doesn't have any result yet, it is still processing in the background. Think of passing around a Thread. But the major difference is - the moment you try to read it, instead of passing it around, it blocks and waits for the result at that point. So in the above example, all the result1 .. resultn variables will keep getting evaluated in the background, but when the time comes to collect the results, and when you try to actually read these values, the reads will wait for the queries to finish at that point.

    Install the promise gem and try the below in Ruby console:

    require 'future'
    x = future { sleep 20; puts 'x calculated'; 10 }; nil
    # adding a nil to the end so that x is not immediately tried to print in the console
    y = future { sleep 25; puts 'y calculated'; 20 }; nil
    
    # At this point, you'll still be using the console!
    # The sleeps are happening in the background
    
    # Now do:
    x + y
    # At this point, the program actually waits for the x & y future blocks to complete
    

    Edit: Typo in result, should have been result1, change echo to puts

提交回复
热议问题