Tl;dr Sinatra works well with Threads, but you will probably have to use a different web server Sinatra itself does not impose any concurrency model, it does not even handle concurrency. This is done by the Rack handler (web server), like Thin, WEBrick or Passenger. Sinatra itself is thread-safe, meaning that if your Rack handler uses multiple threads to server requests, it works just fine.
However, since Ruby 1.8 only supports green threads and Ruby 1.9 has a global VM lock, threads are not that widely used for concurrency, since on both versions, Threads will not run truly in parallel. The will, however, on JRuby or the upcoming Rubinius 2.0 (both alternative Ruby implementations) Most existing Rack handlers that use threads will use a thread pool in order to reuse threads instead of actually creating a thread for each incoming request, since thread creation is not for free, esp. On 1.9 where threads map 1:1 to native threads.
Green threads have far less overhead, which is why fibers, which are basically cooperatively scheduled green threads, as used by the above mentioned sinatra-synchrony, became so popular recently. You should be aware that any network communication will have to go through EventMachine, so you cannot use the mysql gem, for instance, to talk to your database Fibers scale well for network intense processing, but fail miserably for heavy computations. You are less likely to run into race conditions, a common pitfall with concurrency, if you use fibers, as they only do a context switch at clearly defined points (with synchony, whenever you wait for IO).
There is a third common concurrency model: Processes. You can use preforking server or fire up multiple processes yourself. While this seems a bad idea at first glance, it has some advantages: On the normal Ruby implementation, this is the only way to use all your CPUs simultaniously.
And you avoid shared state, so no race conditions by definition. Also, multiprocess apps scale easily over multiple machines. Keep in mind that you can combine multiple process with other concurrency models (evented, cooperative, preemptive) The choice is mainly made by the server and middleware you use: Multi-Process, non-preforking: Mongrel, Thin, WEBrick Multi-Process, preforking: Unicorn, Rainbows, Zbatery, Passenger Evented (suited for sinatra-synchrony): Thin, Rainbow Threaded: Net::HTTP::Server, Threaded Mongrel, Puma.
Tl;dr Sinatra works well with Threads, but you will probably have to use a different web server. Sinatra itself does not impose any concurrency model, it does not even handle concurrency. This is done by the Rack handler (web server), like Thin, WEBrick or Passenger.
Sinatra itself is thread-safe, meaning that if your Rack handler uses multiple threads to server requests, it works just fine. However, since Ruby 1.8 only supports green threads and Ruby 1.9 has a global VM lock, threads are not that widely used for concurrency, since on both versions, Threads will not run truly in parallel. The will, however, on JRuby or the upcoming Rubinius 2.0 (both alternative Ruby implementations).
Most existing Rack handlers that use threads will use a thread pool in order to reuse threads instead of actually creating a thread for each incoming request, since thread creation is not for free, esp. On 1.9 where threads map 1:1 to native threads. Green threads have far less overhead, which is why fibers, which are basically cooperatively scheduled green threads, as used by the above mentioned sinatra-synchrony, became so popular recently.
You should be aware that any network communication will have to go through EventMachine, so you cannot use the mysql gem, for instance, to talk to your database. Fibers scale well for network intense processing, but fail miserably for heavy computations. You are less likely to run into race conditions, a common pitfall with concurrency, if you use fibers, as they only do a context switch at clearly defined points (with synchony, whenever you wait for IO).
There is a third common concurrency model: Processes. You can use preforking server or fire up multiple processes yourself. While this seems a bad idea at first glance, it has some advantages: On the normal Ruby implementation, this is the only way to use all your CPUs simultaniously.
And you avoid shared state, so no race conditions by definition. Also, multiprocess apps scale easily over multiple machines. Keep in mind that you can combine multiple process with other concurrency models (evented, cooperative, preemptive).
The choice is mainly made by the server and middleware you use: Multi-Process, non-preforking: Mongrel, Thin, WEBrick Multi-Process, preforking: Unicorn, Rainbows, Zbatery, Passenger Evented (suited for sinatra-synchrony): Thin, Rainbow Threaded: Net::HTTP::Server, Threaded Mongrel, Puma.
Thanks to make things clear. Good explanation of the different models. Like you mentioned in the third paragraph processes are a good solution and I think widely used for scaling apps.
/ Sub-question: Which setup do you prefer? – asaaki Jun 8 at 18:15 Thanks Konstantin for the elaboration. – Ck- Jun 9 at 4:46 I usually prefer preforking and, if it suits the infrastructure, use evented IO (with callbacks, not with fibers).
I have no issue with callbacks and see no real advantages in em-synchrony/sinatra-synchrony, since they do not implement transparent futures/promises. But that's just me, I guess. – Konstantin Haase Jun 9 at 7:18 Preforking is good, I used it, too.Do you have any documents/articles why callback based evented IO is better than with fibers?
(Should I open a new question for this topic? ) – asaaki Jun 9 at 10:26.
While googling around, found this gem: sinatra-synchrony which might help you, because it touches you question. There is also a benchmark, they did nearly the same thing like you want (external calls). Conclusion: EventMachine is the answer here!
1 I did the benchmark, too. My result was 26.6 secs without and 4.4 secs with sinatra-synchrony gem - that was 16x faster! – asaaki Jun 8 at 15:55 I will try this and hope to update you folks.Thanks.
– Ck- Jun 9 at 4:47.
I've been getting in to JRuby myself lately and I am extremely surprised how simple it is to switch from MRI to JRuby. It pretty much involves swapping out a few gems (in most cases). You should take a look at the combination JRuby and Trinidad (App Server).
Torquebox also seems to be an interesting all-in-one solution, it comes with a lot more than just an app server. If you want to have an app server that supports threading, and you're familiar with Mongrel, Thin, Unicorn, etc, then Trinidad is probably the easiest to migrate to since it's practically identical from the users perspective. Loving it so far!
I figured out that eventually. By default thin is single threaded, you need to enable threaded mode when starting application. – Ck- Sep 12 at 10:06.
After making some changes to code I was able to run padrino/sinatra application on mizuno . Initially I tried to run Padrino application on jRuby but it was simply too unstable and I did not investigate as to why. I was facing JVM crashes when running on jRuby.
I also went through this article, which makes me think why even choose Ruby if deployment can be anything but easy. Is there any discussion on deployment of applications in ruby? Or can I spawn a new thread :).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.