3

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

这是我的 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

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

4

1 回答 1

1

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

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

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

这是关于 ruby​​ 并发的一个很好的解释:http: //merbist.com/2011/02/22/concurrency-in-ruby-explained/

然后检查 puma 服务器,您就会明白我要解释的内容:http: //puma.io/

于 2013-05-16T14:33:59.257 回答