Question

I've a simple Sinatra application with one long running route:

get '/jobs/new' do
  logger.info "jobs/new start. Thread = #{Thread.current.inspect}"
  sleep 10
  logger.info "end new..."
  erb :'jobs/new'
end

get '/jobs' do
  erb :'jobs/index'
end

I've concurrent access between routes, but not to the same route.

An example is, while a client invokes /jobs/new(long during access), another client can invoke jobs in parallel. But the parallel call for the same route doesn't work. In this case, Puma, the webserver, always calls the route with the same thread:

jobs/new started. Thread = #<Thread:0x007f42b128e600 run>
10 seconds later...
jobs/new ended. Thread = #<Thread:0x007f42b128e600 run>
jobs/new started. Thread = #<Thread:0x007f42b128e600 run> <-- new call. Has to wait till first has finished

The other route is being called by different threads. And while route 1 is running:

jobs/new started. Thread = #<Thread:0x007f42b128e600 run>
2 seconds later...
jobs started. Thread = #<Thread:0x007f541f581a40 run> <--other thread
8 seconds later...
jobs/new ended. Thread = #<Thread:0x007f42b128e600 run>
jobs/new started. Thread = #<Thread:0x007f42b128e600 run>

I tried running the app with Thin in threaded mode and with Puma, with the same behavior

Was it helpful?

Solution

Whatever you did, I think it was not right.

Running this code:

# config.ru
require 'bundler'
Bundler.require

get '/jobs/new' do
  logger.info "jobs/new start. Thread = #{Thread.current.inspect}"
  sleep 10
  logger.info "end new..."
  "jobs/new"
end

run Sinatra::Application

with puma:

Puma starting in single mode...
* Version 2.7.1, codename: Earl of Sandwich Partition
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:9292
Use Ctrl-C to stop
I, [2013-12-12T14:04:48.820907 #9686]  INFO -- : jobs/new start. Thread = #<Thread:0x007fa5667eb7c0 run>
I, [2013-12-12T14:04:50.282718 #9686]  INFO -- : jobs/new start. Thread = #<Thread:0x007fa566731e38 run>
I, [2013-12-12T14:04:58.821509 #9686]  INFO -- : end new...
127.0.0.1 - - [12/Dec/2013 14:04:58] "GET /jobs/new HTTP/1.1" 200 8 10.0132
I, [2013-12-12T14:05:00.283496 #9686]  INFO -- : end new...
127.0.0.1 - - [12/Dec/2013 14:05:00] "GET /jobs/new HTTP/1.1" 200 8 10.0015
^C- Gracefully stopping, waiting for requests to finish
- Goodbye

Results in 2 different threads!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top