From: Nabs Kahn Date: 2011-08-23T08:31:54+09:00 Subject: Re: Multithreading in Ruby Thanks Bartosz and Jesús, I implemented both of your suggestions and got it working. For anyone else, this is what the end result looks like: Thread.abort_on_exception = true rank_pages_queue = Queue.new items_queue = Queue.new item_pages_queue = Queue.new finished_items_queue = Queue.new mech_page = get_page(url) rank_page_download = Thread.new do while mech_page != :finish rank_pages_queue << mech_page mech_page = hit_next(mech_page) end rank_pages_queue << :finish end rank_page_extract = Thread.new do while (rank_page = rank_pages_queue.pop) != :finish items_queue << get_rank_data(rank_page) end items_queue << :finish end item_page_download = Thread.new do while (items = items_queue.pop) != :finish items.each do |item_arr| item_pages_queue << [item_arr, get_page(item_arr[3])] end end item_pages_queue << :finish end item_pages_extract = Thread.new do while true item_arr, item_page = item_pages_queue.pop break if item_arr == :finish finished_items_queue << item_arr.concat(get_item_data(item_page)) end finished_items_queue << :finish end write_to_file = Thread.new do while (item_arr = finished_items_queue.pop) != :finish writeToTxt(item_arr, item_arr[0] + ' - ' + filename) end end rank_page_download.join rank_page_extract.join item_page_download.join item_pages_extract.join write_to_file.join -- Posted via http://www.ruby-forum.com/.