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