Ruby Sinatra with consumer thread and job queue

淺唱寂寞╮ 提交于 2019-12-11 11:23:51

问题


I’m trying to create a very simple restful server. When it receives a request, I want to create a new job on a queue that can be handled by another thread while the current thread returns a response to the client.

I looked at Sinatra, but haven't got too far.

require 'sinatra'
require 'thread'

queue = Queue.new

set :port, 9090

get '/' do
  queue << 'item'
  length = queue.size
  puts 'QUEUE LENGTH %d', length
  'Message Received'
end

consumer = Thread.new do
  5.times do |i|
    value = queue.pop(true) rescue nil
    puts "consumed #{value}"
  end
end

consumer.join

In the above example, I know the consumer thread would only run a few times (as opposed to the life of the application), but even this isn't working for me.

Is there a better approach?


回答1:


Your main problem is your call to Queue#pop. You’re passing true, which causes it not to suspend the thread and raises an exception instead, which you rescue with nil. Your consumer thread therefore loops five times before any thing else can happen.

You need to change that line to

value = queue.pop

so that the thread waits for new data being pushed onto the queue.

You’ll also need to remove the consumer.join line from the end, since that will cause deadlock once you’ve changed the call to pop.

(Also, it’s not part of your main problem, but it looks like you want printf rather than puts when you print the queue length).



来源:https://stackoverflow.com/questions/16024197/ruby-sinatra-with-consumer-thread-and-job-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!