我有8个CPU核心和8个独角兽工作人员 - 为什么不同时处理请求?

时间:2013-05-15 22:01:56

标签: ruby-on-rails ruby-on-rails-3 unicorn

我的理解是,默认情况下应该同时处理传入的请求,当我拥有与核心一样多的工作人员时。为什么我看到请求只是同步处理?

这是我的unicorn.rb:

worker_processes 10

APP_PATH = "/var/www/myapp/current" # available in 0.94.0+
APP_PATH_SHARED = "/var/www/myapp/shared"
working_directory APP_PATH
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/.sock", :backlog => 64
listen 8080, :tcp_nopush => true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 60

# feel free to point this anywhere accessible on the filesystem
pid APP_PATH_SHARED + "/pids/unicorn.pid"

# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path APP_PATH_SHARED + "/log/unicorn.stderr.log"
stdout_path APP_PATH_SHARED + "/log/unicorn.stdout.log"



# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = "/var/www/myapp/current/Gemfile"
end

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!


   # Before forking, kill the master process that belongs to the .oldbin PID.
  # This enables 0 downtime deploys.
  old_pid = APP_PATH_SHARED + "/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end

end

after_fork do |server, worker|

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

end

这困扰了我好几个星期!谢谢。

1 个答案:

答案 0 :(得分:1)

您看到请求只是同步处理的原因是因为您没有基于线程的服务器,也可能还有ruby版本。

如果你想要纯粹的并发性,你就不会通过Unicorn获得它。 Unicorn使用进程分叉而不是线程。 如果你想要那种并发性,你应该有线程安全的代码并使用Jruby或rubinius和PUMA。

Puma是一个真正实现基于线程的并发的Web服务器,但为了实现这一点,您必须使用实现线程并发的ruby版本。如果没有,你将再次分叉进程,我认为你不会用puma而不是Unicorn获得任何东西。

这对ruby中的并发性有一个很好的解释:http://merbist.com/2011/02/22/concurrency-in-ruby-explained/

然后检查美洲狮服务器,你就会理解我想要解释的内容:http://puma.io/