[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Thread safety techniques for server applications?

warhero

8/25/2007 2:06:00 AM

Hey all,

I'm looking for some information about handling thread safety with Ruby.
I've got an application server I wrote that I need to make sure it's
thread safe. This application server is used over http requests so it's
possible multiple people hit it at once. I have some questions that will
help me determine..

1. Does using mongrel / lighttpd / webrick, ensure thread saftey? (my
application relies on these)
2. What kinds of things in the Ruby language should I NOT do that will
cause thread headaches.. (maybe static variables)?
3. What techniques can I use to go about testing thread saftey?

I'm not asking anything in reference to rails. This would be just
general Ruby thread safety ideas..

thanks..
--
Posted via http://www.ruby-....

20 Answers

dtuttle1@gmail.com

8/25/2007 7:03:00 AM

0

Hi Aaron,
I'd like to learn more in this area too, but here are my thoughts:
The web servers, at least mongrel, are single-threaded. Mongrel queues
requests and feeds them to the app sequentially. To get concurrency
you have to run multiple instances of mongrel. In this situation there
are no thread safety issues because there's only one thread per
process.
I like the idea of separate processes instead of worrying about thread
safety, but sometimes I need multiple threads, for example in a jabber
client (keepalives, listeners, etc). What I've been doing is keeping
it as simple as possible and so far I haven't had to think about
thread conflicts. Or maybe I should be but I haven't ;)
--Dave

On Aug 24, 7:05 pm, Aaron Smith <beingthexempl...@gmail.com> wrote:
> Hey all,
>
> I'm looking for some information about handling thread safety with Ruby.
> I've got an application server I wrote that I need to make sure it's
> thread safe. This application server is used over http requests so it's
> possible multiple people hit it at once. I have some questions that will
> help me determine..
>
> 1. Does using mongrel / lighttpd / webrick, ensure thread saftey? (my
> application relies on these)
> 2. What kinds of things in the Ruby language should I NOT do that will
> cause thread headaches.. (maybe static variables)?
> 3. What techniques can I use to go about testing thread saftey?
>
> I'm not asking anything in reference to rails. This would be just
> general Ruby thread safety ideas..
>
> thanks..
> --
> Posted viahttp://www.ruby-....


Corey Jewett

8/25/2007 7:23:00 AM

0


On Aug 25, 2007, at 12:05 AM, dtuttle1@gmail.com wrote:

> Hi Aaron,
> I'd like to learn more in this area too, but here are my thoughts:
> The web servers, at least mongrel, are single-threaded. Mongrel queues
> requests and feeds them to the app sequentially. To get concurrency
> you have to run multiple instances of mongrel. In this situation there
> are no thread safety issues because there's only one thread per
> process.

I don't believe this is true of mongrel itself, but rather of the
Rails handler in mongrel.

Corey

khaines

8/25/2007 1:24:00 PM

0

khaines

8/25/2007 1:38:00 PM

0

Terry Poulin

8/25/2007 3:29:00 PM

0

> Remember that Ruby threads, being green threads, are all in the same
> process, so there is no actual concurrency of execution between them. In
> most cases these threads will not increase your throughput.
>
>
> Kirk Haines
>
>
>

I've heard that Ruby 2.0 won't use Green Threads, so hopefully this will
change. I would assume the reason why Ruby uses Green Threads is to try and
maintain portability over efficientcy.

I should play with it, I've never tried Ruby for Multi-Threaded work. It Might
not be worth my time but learning some thing new would be fun.


TerryP.




--

Email and shopping with the feelgood factor!
55% of income to good causes. http://www.ip...


ara.t.howard

8/25/2007 3:59:00 PM

0


On Aug 25, 2007, at 9:29 AM, Terry Poulin wrote:

>
> I've heard that Ruby 2.0 won't use Green Threads, so hopefully this
> will
> change. I would assume the reason why Ruby uses Green Threads is to
> try and
> maintain portability over efficientcy.

it's interesting to me that people assume green threads provide less
performance advantage that native threads. this is patently untrue:
it all depends on your task! to summarize

- if your task is cpu intensive AND you are on an SMP box AND you use
many threads (aka lightweight processes) you will see a speed boost

- if your task is io/network bound and/or you are spawning a TON of
threads then green threads will provide a speedup on any decent (aka
not windoze) platform

consider these facts

- green threads are inexpensive to create compared to native threads
- green threads can help throughput a lot where io is concerened iff
select is a good paradigm for scheduling activity (imagine many
network connections)

- native threads are relatively expensive to create
- native threads have the same bottleneck on io that green threads
have: you can physically only write to disk with the number of disk
controllers you have and reading from sockets may still be limited to
the speed of the person on the other end

green threads are good for something and native threads are good for
somethings. fortunately in ruby it's extremely easy to farm out
tasks to another process and use ipc with

slave = Slave.new{ Server.new }

so we get the best of both works if we want it. people will miss
green threads when they are gone - am i the only one who remembers
not being able to stop java's native threads?

cheers.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




khaines

8/25/2007 7:15:00 PM

0

M. Edward (Ed) Borasky

8/25/2007 7:20:00 PM

0

ara.t.howard wrote:
> consider these facts
>
> - green threads are inexpensive to create compared to native threads
> - green threads can help throughput a lot where io is concerened iff
> select is a good paradigm for scheduling activity (imagine many network
> connections)
>
> - native threads are relatively expensive to create
> - native threads have the same bottleneck on io that green threads have:
> you can physically only write to disk with the number of disk
> controllers you have and reading from sockets may still be limited to
> the speed of the person on the other end
>
> green threads are good for something and native threads are good for
> somethings. fortunately in ruby it's extremely easy to farm out tasks
> to another process and use ipc with
>
> slave = Slave.new{ Server.new }
>
> so we get the best of both works if we want it. people will miss green
> threads when they are gone - am i the only one who remembers not being
> able to stop java's native threads?

Well ... I think we should have *both* green threads (i.e., a built-in
thread scheduler in a single Ruby process) and native threads (i.e., the
Linux "clone" operation creating a separate lightweight process sharing
a memory space). On top of that, we should have Erlang-style lightweight
Ruby processes communicating via message passing, something resembling
MPI and probably something resembling OpenMP. And of course there's dRB
and Rinda -- they aren't going away, are they?

Unfortunately, the whole world doesn't use the Linux kernel and the GCC
compilers, but for the part of the world that *does*, all of this is
doable via C-language libraries, and I'm guessing most of it *has* been
done. I know there's a "ruby-mpi" project, for example, although it
looks like it hasn't been touched in a couple of years and might be
orphaned.

Joel VanderWerf

8/25/2007 7:30:00 PM

0

khaines@enigo.com wrote:
> On Sun, 26 Aug 2007, ara.t.howard wrote:
...
>> - if your task is cpu intensive AND you are on an SMP box AND you use
>> many threads (aka lightweight processes) you will see a speed boost
>
> How do you figure that? On a CPU intensive task, the threading overhead
> just removes from the amount of time that the CPU has to work on the CPU
> intensive task.

On second reading, I *think* what he was saying was one native thread
per cpu.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

8/25/2007 7:36:00 PM

0

M. Edward (Ed) Borasky wrote:
> Well ... I think we should have *both* green threads (i.e., a built-in
> thread scheduler in a single Ruby process) and native threads (i.e., the
> Linux "clone" operation creating a separate lightweight process sharing
> a memory space).

Did the recent discussion of fibers lead to the conclusion that green
threads would still exist in some form in 1.9? Will we be able to
experiment with 1:N and M:N threading in pure ruby?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407