线程如何在红宝石的轨道上工作? [英] How thread works in ruby on rails?

查看:60
本文介绍了线程如何在红宝石的轨道上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,因为我有一段代码要在email_open_order操作中执行,以便我可以在后台运行电子邮件任务并释放GUI.

I need some help as I have a piece of code int rails, that is to be executed in email_open_order action so that I can run the email task in the background and release the GUI.

def email_open_order  
  @order_list = OpenOrder.where(:user_id => current_user)
  t = Thread.new { invoke_webservice method: 'post', action: 'send_open_order', data: { custNo: current_user,custEm: session[:buyer_email], orders: @order_list} }

  at_exit { t.join }
end

invoke_webservice方法调用Web服务,即HTTP POST并将电子邮件发送给用户组.

invoke_webservice method calls the web service, i.e HTTP POST and sends email to group of users.

交付所有未完成订单清单的正常时间为87.12

The normal time it takes to deliver all open order list is 87.12

Benchmark.measure { invoke_webservice method: 'post', action: 'send_open_order', data: { custNo: current_user,custEm: session[:buyer_email], orders: @order_list} }
=> 0.010000 0.000000 0.010000 ( 87.124463)

一旦我创建了一个新线程并将invoke_webservice方法传递给线程块,它就会开始发送邮件并释放GUI.

as soon as I create a new thread and pass the invoke_webservice method to thread block it starts sending mail and GUI is released.

我想知道当代码块的执行完成/失败时线程会发生什么.

I wonder what happens to the thread when the execution of the code block gets completed/fails.

线程是终止还是保持在睡眠模式?

Does the thread terminates or remains in sleep mode?

该线程如何工作?它会创建一个新的PID吗?

How does this thread work? Does it create a new PID?

我使用Unicorn作为我的Web服务器.

I use Unicorn as my web Server.

预先感谢

推荐答案

它不会创建新的PID(因为这将意味着新的进程而不是新的线程).线程完成您的工作后,它将终止.

It does not create a new PID (since that would imply a new process rather than a new thread). When the thread has done your work it will terminate.

典型地,Rails应用程序通过使用诸如delay_job,sidekiq等之类的东西在一个单独的进程中处理这些作业,而不是通过在应用程序内创建线程来执行此操作-通常无法确定请求完成后Web服务器进程将存活多长时间(乘客根据负载启动/杀死应用程序进程),而对这些线程的状态了解不多.

Typically rails apps do this by using something like delayed_job, sidekiq etc. to process these jobs in a separate process rather than by creating threads within the app - it's often uncertain how long the web server process will survive after the request is finished (passenger starts/kills application processes based on load) and you don't get much visibility into the status of these threads.

这篇关于线程如何在红宝石的轨道上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆