0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Collects tasks to be run, then execute in parallel, with maximum specified concurrency.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.12.5, ~> 2.12
~> 3.2
 Project Readme

parallel_tasker

Small Ruby Gem to run tasks in parallel.

This is useful when you need to parallelize tasks with concurrency limit, and does not want to deal with Threads.

Example

With block (recommended)

Processing each task return

require 'parallel_tasker'

concurrency = 2
number_of_tasks = 5

# Creates a new object
ParallelTasker.new(concurrency) do |pt|
  # Add tasks
  (1..number_of_tasks).each do |id|
    pt.add_task(id) do
      puts "#{id}: Starting..."
      sleep id
      # Let's raise on last task
      if id == number_of_tasks
        raise "Exception at task #{id}!"
      end
      puts "#{id}: Finished!"
      id
    end
  end

  # Run all tasks (this will block until last thread finishes)
  puts 'Running all tasks.'
  statuses = pt.run
  puts 'Finished all tasks.'
  puts

  # Get each task status.
  puts "Returns"
  statuses.each do |id, thread|
    print "#{id}: "
    message = begin
      "Returned: #{thread.value.to_s}"
    rescue
      "Raised #{$!.class}: #{$!}"
    end
    puts message
  end
end

Ignoring all tasks return

require 'parallel_tasker'

concurrency = 2
number_of_tasks = 5

# Creates a new object
ParallelTasker.new(concurrency) do |pt|
  # Add tasks
  (1..number_of_tasks).each do |id|
    pt.add_task(id) do
      puts "#{id}: Starting..."
      sleep id
      # Let's raise on last task
      if id == number_of_tasks
        raise "Exception at task #{id}!"
      end
      puts "#{id}: Finished!"
      id
    end
  end

Without block

require 'parallel_tasker'

concurrency = 2
number_of_tasks = 5

# Creates a new object
pt = ParallelTasker.new(concurrency)

# Add tasks
(1..number_of_tasks).each do |id|
  pt.add_task(id) do
    puts "#{id}: Starting..."
    sleep id
    # Let's raise on last task
    if id == number_of_tasks
      raise "Exception at task #{id}!"
    end
    puts "#{id}: Finished!"
    id
  end
end

# Run all tasks (this will block until last thread finishes)
puts 'Running all tasks.'
statuses = pt.run
puts 'Finished all tasks.'
puts

# Get each task status.
puts "Returns"
statuses.each do |id, thread|
  print "#{id}: "
  message = begin
    "Returned: #{thread.value.to_s}"
  rescue
    "Raised #{$!.class}: #{$!}"
  end
  puts message
end