Multithreading in ruby

K

Kaja Mohaideen

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
 
S

saurabh purnaye

[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 <
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


--
--
Thanks and Regards
Saurabh Purnaye
+91-9922907342
skype: sorab_pune
yahoo & gtalk: saurabh.purnaye
msn: (e-mail address removed)
 
J

James Dinkel

Kaja said:
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.
 
C

Charles Oliver Nutter

saurabh said:
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
 
C

Charles Oliver Nutter

James said:
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
 
G

Giacomo Graziosi

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-mvm-research ), 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top