【问题标题】:Ruby 1.9.3, Rails 3.2.12, OSX Lion - bootstrapping Rails for command-line scripts slowRuby 1.9.3、Rails 3.2.12、OSX Lion - 命令行脚本的引导 Rails 很慢
【发布时间】:2013-03-07 05:11:59
【问题描述】:

在命令行脚本中引导 Rails 需要很长时间(2.5 秒以上), 例如:

timer = Time.now  
ENV["RAILS_ENV"] ||= "development"  
require File.expand_path('../../config/application', __FILE__)  
Rails.application.require_environment!  
puts "Init complete in #{Time.now - timer} sec"  
# -> Init complete in 2.621531 sec  

我错过了什么吗?如果不是,我真的需要引导所有东西来访问模型吗?

【问题讨论】:

  • 您也可以只调用:require 'config/environment',它应该执行您需要的各种环境加载。然而,它不会加速你已经拥有的东西。请参阅下面的建议。

标签: ruby-on-rails ruby command-line bootstrapping


【解决方案1】:

我在这方面没有太多经验,但也许你应该看看减少 required in application.rb 库的数量:

# require 'rails/all' # remove this and only require what you need
require 'activerecord/railtie' # this may be all you need

此外,请查看您正在加载的 gem 和 see if any of them are taking an inordinate amount of time。如果某个 gem 正在减慢启动时间,只需添加:

# you will need to explicitly load the gem when needed,
# but it won't be required at boot time 
gem some_gem, :require => false

您可以创建一个运行脚本的特殊环境,并为该环境自定义引导过程。

您应该分析该代码块并查看大部分时间都花在了哪里。

您究竟想从引导轨道中得到什么?访问模型?基础数据?也许有一种替代方法,而不是引导所有的 rails。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    相关资源
    最近更新 更多