[Mongrel] multi threaded theoretically useful?

Kirk Haines wyhaines at gmail.com
Fri Sep 7 18:34:49 EDT 2007

On 9/7/07, Erik Hetzner <erik.hetzner at ucop.edu> wrote:

> > And when you are waiting on something like a database, you are doing
> > it within the DB driver extension, so there are no context switches
> > happening -- your green threads are blocked.
> Surely it is possible to write a C extension for Ruby that allows
> context switching from within it? This is outside my understanding of
> Ruby threads.

Maybe, but it's irrelevant.  The MySQL/Pg/whatever drivers aren't
written to be aware of Ruby internals.

> > The reason you get more performance when multithreading is because
> > of that sleep call. It is all happening inside of Ruby, so when
> > running in a multithreaded context, when one thread goes to sleep,
> > the others just get the execution cycles. It's artificial, though.
> > Read Ruby web apps don't tend to have latencies like that. When they
> > are waiting, they are waiting inside of things that block the entire
> > Ruby process, and not just waiting for something that is blocking a
> > single thread.
> I know why we are getting greater performance, and again, I don't
> think it's artificial. But, in any case, it was a demonstration,
> mostly for the original responder, who claimed that you would never
> get more performance from Ruby threads.

Ezra?  His response simplified it.  Of course there are situations
where green threads improve performance.  They can be quantified very
simply, as I already have.  When there are external latencies that are
not blocking the entire process, green threads can capture those
latencies and turn them into additional work.  A sleep() call is such
a latency.  Any pure ruby code that stays in rubyland and doesn't go
into an external extension is such a latency.  A DB query that spends
most of its time inside of some C library that is external to Ruby is
not such a latency.

> Again, your mileage may vary. And it's all moot for rails users since
> I don't see rails going multithreaded.

It's not very difficult, actually, to do it.  Just make a copy of the
rails.rb mongrel handler for Rails.  Remove the bit that wraps the
call to Dispatcher.dispatch() in a @guard.synchronize block.

Whether all of Rails is actually threadsafe, and whether one's code
is....that's another thing, but it's easy to run Rails in multiple
threads under Mongrel if one wants to play with that.

Kirk Haines

More information about the Mongrel-users mailing list