English 中文(简体)
如何在铁路3中使用多管线?
原标题:How to use multi-threading in rails 3?

我通过邮递向用户发送邮件。 这是我的控制人员守则:

@users = User.where(some condition)
@product = Product.find_by_name(some name).first
for user in @users
  UserMailer.new_product_arrival(user, @product, home_url).deliver
end

以及我的用户——电子邮箱:rb

def new_product_arrival(user,product,home_url)
  @from         = Settings.mailer_from_address
  @recipients   = user.login
  @sent_on      = Time.now
  @user = user
  @product = product
  @content_type = "text/html"
  @home_url = home_url
end

问题是,如果用户超过10个,则由于<<><>条码/代码>的路程而需要很长时间。 我需要知道,我们是否能够通过利用多读或背景的工作来处理这一问题。 我不想利用背景工作,但任何人都能够告诉我如何使用多读手段执行上述工作。

我正在使用废墟1.8.7和铁路3.07。

最佳回答

为了达到“多读”的目的,基本上有两个办法:

  1. 每一次分娩的配对器,并重新加入主线

    threads = []
    for user in @users
       threads << Thread.new do
         UserMailer.new_product_arrival(user, @product, home_url).deliver
       end
    end
    threads.each(&:join)
    
  2. a. 在整个铁路上铺设叉(但满足请求的铁路将立即做出反应)并且使这一过程脱节:

    process = fork do
      for user in @users
        UserMailer.new_product_arrival(user, @product, home_url).deliver
      end
      Process.kill("HUP") 
      #sends the kill signal to current Process, which is the Rails App sending your emails 
    end
    Process.detach(process)
    

Hope that helps

问题回答




相关问题
Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Is reading from an XmlDocument object thread safe?

I was wondering if i could safely read from an XmlDocument object using SelectNodes() and SelectSingleNode() from multiple threads with no problems. MSDN says that they are not guaranteed to be ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签