[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Multithreading in ruby

Kaja Mohaideen

8/14/2008 4:59:00 AM

We people are new team for ruby. we trying to pput multithreading in our
code. how is it possible? we want to read a message from queue and
process that message parellaly and finally insert the message to the DB.
Can you give me the Idea please

Regards
Kaja Mohaideen.A
Trichy
--
Posted via http://www.ruby-....

5 Answers

saurabh purnaye

8/14/2008 5:10:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Threads and ProcessesMultithreading

Often the simplest way to do two things at once is by using *Ruby threads*.
These are totally in-process, implemented within the Ruby interpreter. That
makes the Ruby threads completely portable---there is no reliance on the
operating system---but you don't get certain benefits from having native
threads. You may experience thread starvation (that's where a low-priority
thread doesn't get a chance to run). If you manage to get your
threadsdeadlocked, the whole process may grind to a halt. And if some
thread happens to make a call to the operating system that takes a long time
to complete, all threads will hang until the interpreter gets control back.
However, don't let these potential problems put you off---Ruby threads are a
lightweight and efficient way to achieve parallelism in your code. Creating
Ruby Threads

Creating a new thread is pretty straightforward. Here's a simple code
fragment that downloads a set of Web pages in parallel. For each request
it's given, the code creates a separate thread that handles the HTTP
transaction.

require 'net/http'


pages = %w( www.rubycentral.com
www.awl.com
www.pragmaticprogrammer.com
)

threads = []


for page in pages
threads << Thread.new(page) { |myPage|


h = Net::HTTP.new(myPage, 80)
puts "Fetching: #{myPage}"
resp, data = h.get('/', nil )
puts "Got #{myPage}: #{resp.message}"
}
end

threads.each { |aThread| aThread.join }

*produces:*

Fetching: www.rubycentral.com
Fetching: www.awl.com
Fetching: www.pragmaticprogrammer.com
Got www.rubycentral.com: OK
Got www.pragmaticprogrammer.com: OK
Got www.awl.com: OK

You may see the book came with installation of ruby, in /ruby/doc folder

On Thu, Aug 14, 2008 at 10:29 AM, Kaja Mohaideen <
kajamohaideen_2003@yahoo.co.in> wrote:

> We people are new team for ruby. we trying to pput multithreading in our
> code. how is it possible? we want to read a message from queue and
> process that message parellaly and finally insert the message to the DB.
> Can you give me the Idea please
>
> Regards
> Kaja Mohaideen.A
> Trichy
> --
> Posted via http://www.ruby-....
>
>


--
--
Thanks and Regards
Saurabh Purnaye
+91-9922907342
skype: sorab_pune
yahoo & gtalk: saurabh.purnaye
msn: psaurabh@live.com

James Dinkel

8/14/2008 5:11:00 AM

0

Kaja Mohaideen wrote:
> We people are new team for ruby. we trying to pput multithreading in our
> code. how is it possible? we want to read a message from queue and
> process that message parellaly and finally insert the message to the DB.
> Can you give me the Idea please
>
> Regards
> Kaja Mohaideen.A
> Trichy

Use the Thread class. Just be aware that Ruby 1.8.x uses green threads.
Ruby 1.9.x uses native threads.
--
Posted via http://www.ruby-....

Charles Oliver Nutter

8/14/2008 5:17:00 AM

0

saurabh purnaye wrote:
> Threads and ProcessesMultithreading
>
> Often the simplest way to do two things at once is by using *Ruby threads*.
> These are totally in-process, implemented within the Ruby interpreter. That
> makes the Ruby threads completely portable---there is no reliance on the
> operating system---but you don't get certain benefits from having native
> threads. You may experience thread starvation (that's where a low-priority
> thread doesn't get a chance to run). If you manage to get your
> threadsdeadlocked, the whole process may grind to a halt. And if some
> thread happens to make a call to the operating system that takes a long time
> to complete, all threads will hang until the interpreter gets control back.
> However, don't let these potential problems put you off---Ruby threads are a
> lightweight and efficient way to achieve parallelism in your code.

As has been discussed here many times before, neither Ruby 1.8.6 nor
Ruby 1.9 run threads in parallel. The only implementations currently
that have actual parallel threads are JRuby (for sure) and IronRuby (I
think). So if having things process *actually* in parallel is important
for your app you may want to consider JRuby.

- Charlie

Charles Oliver Nutter

8/14/2008 5:17:00 AM

0

James Dinkel wrote:
> Kaja Mohaideen wrote:
>> We people are new team for ruby. we trying to pput multithreading in our
>> code. how is it possible? we want to read a message from queue and
>> process that message parellaly and finally insert the message to the DB.
>> Can you give me the Idea please
>>
>> Regards
>> Kaja Mohaideen.A
>> Trichy
>
> Use the Thread class. Just be aware that Ruby 1.8.x uses green threads.
> Ruby 1.9.x uses native threads.

Ruby 1.9 has native threads, but they do not run in parallel and Ruby is
still in charge of scheduling them (or allowing the OS to schedule them).

- Charlie

Giacomo Graziosi

8/14/2008 10:30:00 AM

0

On Aug 14, 7:17=A0am, Charles Oliver Nutter <charles.nut...@sun.com>
wrote:
> Ruby 1.9 has native threads, but they do not run in parallel and Ruby is
> still in charge of scheduling them (or allowing the OS to schedule them).
>

What are they planning for the future? Some time ago a joined research
project between Tokyo University and Sun was annunced (
http://www.infoq.com/news/2008/02/ruby-mv... ), did they
release anything yet? Does anybody know if the guys developing the
main Ruby implementations (MRI and Yarv) are planning something about
this?
Looks like both JRuby and Rubinius are sharing a common API for MVM
and JRuby has support for concurrent threading too: I feel the lack
for a common specification here, are we supposed to write Ruby code
that won't work on every Ruby implementation?