如何/何时“after_filter”工作/运行?

发布于 2025-01-08 13:12:48 字数 478 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2025-01-15 13:12:48

您必须知道对服务器的请求是完全一致的并且在其自己的线程中运行。

如果将代码放入 after_filter 中,这将延迟整个请求:总而言之,如果线程处于活动状态,则不会传递页面。

这就是为什么您会发现 DelayedJobResque 等出色的插件,它们在并行进程中工作,不会影响您的应用程序。


为了回答您原来的问题,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 and Resque, 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.

新人笑 2025-01-15 13:12:48

after_filter 在内容生成之后、发送到客户端之前运行。您可以验证这一点,因为您可以在任何 after_filter 中的控制器的 response 方法中访问完全生成的响应数据。

After_filters 不适合放置长时间运行的任务,因为它们肯定会影响用户的体验。考虑使用 after_filter 来启动长时间运行的 delayed_jobresque 任务来执行后台工作。

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 the response 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文