混淆了 rails 4 中的 autoload_paths 与eager_load_paths [英] confusing about autoload_paths vs eager_load_paths in rails 4

查看:52
本文介绍了混淆了 rails 4 中的 autoload_paths 与eager_load_paths的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一篇关于 rails load_paths 的文章,这是链接.

I read a post about the rails load_paths, here is the link.

但是,我仍然对 autoload_pathseager_load_paths 之间的区别感到困惑:

But, I am still confused about the difference between the autoload_paths and eager_load_paths:

我已经在一个新创建的 Rails 4 项目中测试了它们.它们似乎以相同的方式运行,即在开发模式下自动重新加载,但在生产模式下.

I have tested them in a newly created Rails 4 project. It seems that they run the same way, that auto-reload in the development mode but in the production mode.

推荐答案

此处链接文章的作者.这是试图消除混淆,从@fkreusch 的回答开始.

Author of the linked article here. Here's an attempt to clear up the confusion, going off of @fkreusch's answer.

在 Ruby 中,您必须要求每个 .rb 文件才能运行其代码.但是,请注意在 Rails 中,您从不特别需要 app/ 目录中的任何模型、控制器或其他文件.这是为什么?那是因为在 Rails 中 app/*autoload_paths 中.这意味着当您在开发中运行 Rails 应用程序时(例如通过 rails 控制台)— ruby​​ 实际上还不需要任何模型和控制器.Rails 使用 ruby​​ 的特殊神奇特性来实际等待代码提到一个常量,比如 Book,然后它才会运行 require 'book' 它在其中一个中找到autoload_paths.这可以让您在开发中更快地启动控制台和服务器,因为在您启动它时什么都不需要,只有在代码实际需要时才需要.

In Ruby you have to require every .rb file in order to have its code run. However, notice how in Rails you never specifically require any of your models, controllers, or other files in the app/ dir. Why is that? That's because in Rails app/* is in autoload_paths. This means that when you run your rails app in development (for example via rails console) — none of the models and controllers are actually required by ruby yet. Rails uses special magical feature of ruby to actually wait until the code mentions a constant, say Book, and only then it would run require 'book' which it finds in one of the autoload_paths. This gives you faster console and server startup in development, because nothing gets required when you start it, only when code actually needs it.

现在,这种行为有利于本地开发,但是生产呢?想象一下,在生产环境中,您的服务器执行相同类型的神奇常量加载(自动加载).这真的不是世界末日,您在生产中启动服务器,人们开始浏览您的页面会稍微慢一些,因为某些文件需要自动加载.是的,当服务器预热"时,这几个初始请求会变慢,但并没有那么糟糕.不过,这还不是故事的结局.

Now, this behavior is good for local development, but what about production? Imagine that in production your server does the same type of magical constant loading (autoloading). It's not the end of the world really, you start your server in production, and people start browsing your pages slightly slower, because some of the files will need to be autoloaded. Yes, it's slower for those few initial requests, while the server "warms up", but it's not that bad. Except, that's not the end of the story.

如果您在 ruby​​ 1.9.x 上运行(如果我没记错的话),那么像这样的自动请求文件不是线程安全的.所以如果你使用像 puma 这样的服务器,你会遇到问题.即使您没有使用多线程服务器,您仍然最好在启动时主动"要求整个应用程序.这意味着在生产中,您希望在启动应用程序时完全需要每个模型、每个控制器等,并且您不介意更长的启动时间.这称为急切加载.所有 ruby​​ 文件都被急切地加载,明白了吗?但是,如果您的 rails 应用程序没有一个 require 语句,您怎么能做到这一点呢?这就是 eager_load_paths 的用武之地.无论您在其中放入什么,这些路径下的所有目录中的所有文件都将在生产启动时需要.希望这能解决问题.

If you are running on ruby 1.9.x (if I recall correctly), then auto-requiring files like that is not thread safe. So if you are using a server like puma, you will run into problems. Even if you aren't using a multi-threaded server, you are still probably better off having your whole application get required "proactively", on startup. This means that in production, you want every model, every controller, etc all fully required as you start your app, and you don't mind the longer startup time. This is called eager loading. All ruby files get eagerly loaded, get it? But how can you do that, if your rails app doesn't have a single require statement? That's where eager_load_paths come in. Whatever you put in them, all the files in all the directories underneath those paths will be required at startup in production. Hope this clears it up.

需要注意的是,eager_load_paths 在开发环境中是不活跃的,所以你在它们中放入的任何东西都不会在开发中立即被迫切需要,只有在生产中.

It's important to note that eager_load_paths are not active in development environment, so whatever you put in them will not be eagerly required immediately in development, only in production.

同样重要的是要注意,仅将某些内容放入 autoload_paths 不会使其在生产中立即加载.很遗憾.您还必须明确将其放入 eager_load_paths 中.

It's also important to note that just putting something into autoload_paths will not make it eager-loaded in production. Unfortunately. You have to explicitly put it into eager_load_paths as well.

另一个有趣的怪癖是在每个 Rails 应用程序中,app/ 下的所有目录都会自动在 autoload_pathseager_load_paths 中,这意味着添加不需要进一步操作的目录.

Another interesting quirk is that in every rails app, all directories under app/ are automatically in both autoload_paths and eager_load_paths, meaning that adding a directory there requires no further actions.

这篇关于混淆了 rails 4 中的 autoload_paths 与eager_load_paths的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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