Skip to content

cetinkaya/paralleldo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

paralleldo

Paralleldo provides a simple ruby function named "parallel" to execute tasks in parallel (?) through a number of threads. (? : Execution may or may not be actually in parallel depending on the ruby implementation you use. See http://stackoverflow.com/a/57802 )

Usage

require_relative "paralleldo"
array_of_outputs = parallel(number_of_threads, array_of_inputs) do |thread_no, input|
  # do stuff with input in thread #thread_no
  # calculate output from input
  # return output
end

Example

Suppose we want to list some events that happened in October 2000. We can get a list of events from Wikipedia page http://en.wikipedia.org/wiki/October_2000 or we can compile events ourselves from pages http://en.wikipedia.org/wiki/October_1 , http://en.wikipedia.org/wiki/October_2 , ...

Let's follow the second approach where we access 31 wikipedia pages corresponding to each day of October. (It seems like we can get more events this way.) Below is a code to do this with 5 threads using paralleldo.

require_relative "paralleldo"
require "open-uri"

days = (1..31).to_a
results = parallel(5, days) do |thread_no, day|
  url = "http://en.wikipedia.org/wiki/October_#{day}"

  # If wikipedia article in -url- mentions an event
  # for the day October -day-, 2000, return a string
  # which represents that event

  text = open(url).read
  pos = text.index("/wiki/2000")
  if pos
    # Event mentioned in Wikipedia without html tags
    text[text.index(" – ", pos)..
         text.index("</li>", pos)-1].gsub(" – ", "").gsub(/<[^>]+>/, "")
  end
end

puts "Events mentioned in Wikipedia: "
results.each.with_index do |event, index|
  day = days[index]

  if event
    puts "October #{day}, 2000: #{event}"
  end
end

About

A simple ruby function to execute tasks in parallel through a number of threads.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages