0.0
No release in over a year
EachInThread extends Ruby's Enumerable module to provide a simple, efficient way to run tasks in parallel by creating separate threads for each item in an enumerable.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

= 0.1.0
 Project Readme

EachInThread

Gem Version

The each_in_thread gem extends Ruby's Enumerable module to provide an easy way to iterate over collections with concurrency. This means you can perform actions on each item in an array (or any other enumerable) in separate threads.

Installation

Add this line to your application's Gemfile:

gem 'each_in_thread'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install each_in_thread

Usage

require 'each_in_thread'
require 'net/http'

# Suppose we have an array of URLs we want to fetch.
urls = %w[http://example.com http://example.org http://example.net]

# We can fetch each URL in a separate thread like this:
urls.each_in_thread(concurrency: 5) do |url, i|
  response = Net::HTTP.get(URI(url))
  puts "(#{i}) Fetched #{url}: #{response.size} bytes"
end

The each_in_thread method takes an options hash, where you can specify the following:

  • concurrency: The number of threads to use. Defaults to 10.
  • log_each: The number of iterations between each progress output. Defaults to 1.
  • verbose: Whether to output progress. Defaults to false.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Galaxy83/each_in_thread. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.