Thread safe Enumerator in Ruby

后端 未结 1 367
别跟我提以往
别跟我提以往 2021-01-05 03:52

TLDR: Is there a thread-safe version of the Enumerator class in Ruby?


What I\'m trying to do:

I have a method in a Ruby On Rails application that I

相关标签:
1条回答
  • 2021-01-05 04:03

    If you start the worker threads before filling up the queue, they will start consuming the queue as you fill it up, and because as a rule of thumb - network is slower than CPU, each batch should be (mostly) consumed by the time the next batch arrives:

    queue = Queue.new
    
    t1 = Thread.new do
      while !queue.empty?
        p queue.pop(true)
        sleep(0.1)
      end
    end
    t2 = Thread.new do
      while !queue.empty?
        p queue.pop(true)
        sleep(0.1)
      end
    end
    
    (0..1000).map(&queue.method(:push))
    
    t1.join
    t2.join
    

    If that proves too slow still, you can opt to use SizedQueue, which will block the push if the queue reaches a big enough size:

    queue = SizedQueue.new(100)
    
    t1 = Thread.new do
      while !queue.empty?
        p "#{queue.pop(true)} - #{queue.size}"
        sleep(0.1)
      end
    end
    t2 = Thread.new do
      while !queue.empty?
        p queue.pop(true)
        sleep(0.1)
      end
    end
    (0..300).map(&queue.method(:push))
    t1.join
    t2.join
    
    0 讨论(0)
提交回复
热议问题