web services - WebService Concurrency on Rails -


i have simple app , want use webservice.

my problem can't receive more 1 request @ same time.

apparently, requests enqueued , executed 1 one. so, if make 2 requests on same url, second has wait first one.

i've tried use unicorn, puma , thin enable concurrency on requests, seems keep queuing requests url.

example:

  • i make request 1 @ localhost:3000/example
  • i make request @ localhost:3000/another_example
  • i make last request @ localhost:3000/example

the first , second requests executed concurrently, last 1 (that has same url first) has wait first finish.

unicorn, puma , thin enable concurrency, on different urls.

notes:

i added on config/application.rb:

config.allow_concurrency = true 

i'm running app with:

rails s puma 

how can perform requests concurrently?

you're right, each puma/thin/unicorn/passenger/webrick workers houses single rails app instance (or sinatra app instance, etc) per ruby process. it's 1 web worker = 1 app instance = 1 ruby process.

each request blocks process until response ready. it's 1 request per process.

ruby has called "gil" (global interpreter lock) blocks execution of multiple threads because of c extensions lack of thread-safe controles such mutexes , semaphores. means threads won't run concurrently. in practice, "can". i/o operations can block execution waiting response. example, reading file or waiting response network socket. in case, ruby allow thread resume until i/o operation of previous thread finishes.

but rails used have single block of execution per request well, it's own lock. in rails 3, added thread-safe controls through rails code ensure run in jruby example. , in rails 4 decided have thread-safe controls on default.

in theory means more 1 request can run in parallel in ruby mri (as supports native threads since 1.9). in practice 1 request can run while waiting database process return example. should see few more requests running in parallel. if example cpu bound (more internal processing i/o blocks) effect should if requests running 1 after other. now, if have more i/o blocks (as waiting large sql select return), should see running more in parallel (not though).

you see parallel requests more if use virtual machine not native threads no global interpreter lock, such case of jruby. recommend using jruby puma.

puma , passenger both multi-threaded. unicorn fork-based. thin eventmachine based. i'd recommend testing passenger well.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -