Multithreading in ruby

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 M…A
Trichy

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 M. <
[email protected]> wrote:

Thanks and Regards
Saurabh P.
+91-9922907342
skype: sorab_pune
yahoo & gtalk: saurabh.purnaye
msn: [email protected]

Kaja M. 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 M…A
Trichy

Use the Thread class. Just be aware that Ruby 1.8.x uses green threads.
Ruby 1.9.x uses native threads.

saurabh purnaye wrote:

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

James D. wrote:

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

On Aug 14, 7:17 am, Charles Oliver N. [email protected]
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 (
Common Ruby MVM API research kicked off ), 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?