Skip to content

shyouhei/sized_parallel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This library lets you have a bunch of inter-dependent tasks run in parallel, but not all together. Much like make -j4 runs at most 4 tasks at once.

Detailed usage

This library has only one intuitive usage. First create a SizedParallel instance.

sp = SizedParallel.new 65535

Omit the argument to use the active CPU count (default).

Then add your series of jobs.

128.times {
  sp.start {
      Resolv.getaddress 'www.ruby-lang.org'
  }.then { |addr|
      Net::HTTP.get addr, '/'
  }.then { |html|
      Nokogiri::HTML html
  }
}

In the example above 128 concurrent name resolution are registered. Then for each resolved address, HTTP query is registered (in parallel). And lastly the response HTML is registered to parse (also in parallel).

Those jobs are not triggered yet. In order to do so you call a wait for the object.

sp.wait

This blocks, until all the registered jobs finish either gracefully or unexpectedly.

Restrictions

This library is written in pure ruby so ultra portable by nature, but requires at least version 2.2 or higher (or compatible alternatives) because Etc.nproessors is mandatory.

About

A core library that lets you do something like `make -j4`.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages