如何异步运行少量代码? [英] How to run a very small amount of code asynchronously?

查看:83
本文介绍了如何异步运行少量代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Rails应用程序中异步运行少量代码.该代码记录了一些已知信息.我希望此任务不会阻止我的应用程序的其余响应.该操作过于轻巧且过于频繁,无法作为延迟的工作任务来完成.

I'd like to run a small amount of code asynchronously in my rails application. The code logs some known information. I'd like this task to not block the rest of the response from my app. The operation is way too lightweight and frequent for it to be done as a delayed job tasks.

我正在考虑只使用:

Thread.new do
  # my logging code
end

并称之为一天.这实现了我想要实现的目标吗?有什么缺点吗?

and call it a day. Is this achieving what I want it to achieve? Are there any drawbacks?

推荐答案

除了处理一些基准资源争用之外,这还足够.如果您要在其中登录,就会想到数据库资源;如果您在此登录,则想到一个文件.但是我认为基本方法很好.越简单越好...

Aside from handling some baseline resource contention things, this should be sufficient. The database resource comes to mind if you're logging there...a file if you're logging there. But the basic approach is fine I would think. Simpler is better...

这篇关于如何异步运行少量代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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