2013-05-15 51 views
3

我的理解是,當我擁有與我的內核一樣多的工作人員時,應該默認處理傳入請求。爲什麼我看到請求只能同步處理?我有8個CPU核心和8個獨角獸工作者 - 爲什麼不同時處理請求?

這裏是我的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 

這已經困擾了我好幾個星期了!謝謝。

+0

你可以張貼一些輸出被展示請求正在同步處理而不是同時處理? –

+0

您是否正在生產? – jokklan

+0

你使用的ruby版本是什麼? – tbem

回答

1

你看到的請求只能同步處理的原因是因爲你沒有基於線程的服務器,也可能是ruby版本。

如果你想純粹的併發性,你不會通過獨角獸得到它。獨角獸與進程分叉而不是線程一起工作。 如果你想要這種併發性,你應該有線程安全的代碼,並使用PUMA的Jruby或rubinius。

Puma是一個真正實現基於線程的併發性的Web服務器,但爲了實現您必須使用實現線程併發性的Ruby版本。如果沒有,你會再次分叉進程,我認爲這樣你就不會用彪馬而不是麒麟獲得任何東西。

這是在紅寶石關於併發的一個很好的解釋:http://merbist.com/2011/02/22/concurrency-in-ruby-explained/

然後檢查彪馬服務器,你就會明白我試圖解釋:http://puma.io/

+0

你明白了嗎? – tbem

相關問題