如何/何时“after_filter”工作/运行?
我正在使用 Ruby on Rails 3.1,我想知道(出于性能原因)after_filter
是否在渲染视图文件后运行。也就是说,当用户访问我的应用程序 URL 时,他/她应该显示的相关视图文件会在 after_filter
运行之前渲染,或者 after_filter
在运行之前渲染。视图文件已渲染?
换句话说,应用服务器在运行after_filter
之前开始将渲染的视图数据发送给用户,或者等待运行 after_filter
方法,然后它才会发送该视图数据?
PS:我打开这个问题是因为我想运行一些系统更新(注意:这些更新不会影响视图输出数据,也不会“由视图使用”/“完全需要”视图),而不影响最终用户体验(例如:我的应用程序网页加载缓慢)。
I am using Ruby on Rails 3.1 and I would like to know (for performance reasons) whether or not the after_filter
runs after that view files are rendered. That is, when an user access a my application URL, the related view file that he/she should display is rendered before that the after_filter
runs or the after_filter
runs before that the view file is rendered?
In other words, the application server starts to send the rendered view data to the user before to run the after_filter
or it waits to run the after_filter
method and only then it sends that view data?
P.S.: I opened this question because I would like to run some system updates (note: these updates doesn't affect the view output data and are not "used by"/"necessary to" the view at all) without impact on the end user experience (for example: slow loading of my application Web pages).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须知道对服务器的请求是完全一致的并且在其自己的线程中运行。
如果将代码放入
after_filter
中,这将延迟整个请求:总而言之,如果线程处于活动状态,则不会传递页面。这就是为什么您会发现
DelayedJob
和Resque
等出色的插件,它们在并行进程中工作,不会影响您的应用程序。为了回答您原来的问题,
after_filter
在视图处理后被触发。这有时会很麻烦,这就是为什么人们创建了一些
before_render< /code> 过滤器。
You have to be aware that a request to your server is fully consistent and runs in its own thread.
If you put code in an
after_filter
, this will delay the whole request: to sum up badly, if the thread is alive, the page is not delivered.That's why you find great plugins like
DelayedJob
andResque
, they work in a parallel processes and won't affect your app.To answer your original question ,
after_filter
are triggered after the view has been processed.This is sometimes a hassle that's why people created some
before_render
filters.after_filter
在内容生成之后、发送到客户端之前运行。您可以验证这一点,因为您可以在任何 after_filter 中的控制器的response
方法中访问完全生成的响应数据。After_filters 不适合放置长时间运行的任务,因为它们肯定会影响用户的体验。考虑使用 after_filter 来启动长时间运行的 delayed_job 或 resque 任务来执行后台工作。
after_filter
is run after the content has been generated, but before it's been sent to the client. You can verify this is so because you have access to the completely-generated response data in theresponse
method of your controller in any after_filter.After_filters are inappropriate places to put long running tasks as they will definitely impact the users' experience. Consider instead using the after_filter to kick off a long running delayed_job or resque task to perform your background work instead.