/forkomatic

Easily run multiple processes continuously in parallel.

Primary LanguageRubyMIT LicenseMIT

forkomatic

Description

  • forkomatic makes parallel processing extremely simple and powerful.

  • forkomatic can be used to quickly process data with ‘n’ processes.

  • forkomatic can also maintain a set number of processes for any number of iterations, adding new processes as child processes finish.

Synopsis

class YourForkomaticClass < Forkomatic
  class Job < Forkomatic::Job
    attr_accessor :data

    def initialize(data)
      self.data = data
    end

    def work!
      puts "Working on #{data}"
      sleep 1
    end
  end

  def build_jobs(count)
    (1..count).each.collect {|i| YourForkomaticClass::Job.new(i)}
  end
end

# To just create 20 child processes:
Forkomatic.new(20)

# 10 child processes will be maintained, and the loop will will run twice, waiting 1 second between iterations.
Forkomatic.new({:max_children => 10, :work_interval => 1, :max_iterations => 2})

# The work function will run continuously, adding new child processes as old ones die to maintain a constant 10 processes.
Forkomatic.new({:max_children => 10, :work_interval => 1})
  • Override the Job initialize function to give your child processes data to work with.

  • Override the Job work! function to do whatever processing is required.

  • Override the Forkomatic build_jobs() function to create ‘count’ child processes as needed.

  • Create a new instance of your Forkomatic class by giving it a number of processes to create or manually specifying parameters.

Examples

  • Examples are found in the test/ directory

Contributing to forkomatic

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Related projects

forkify: github.com/dakrone/forkify

  • based on forkoff and threadify, used to processes a bunch of data using ‘n’ processes. Limited to one iteration, but very simple to use.

forkoff: github.com/ahoward/forkoff

  • works on enumerable objects, iterating a code block to run in each child processes and collecting results. Also limited to a single iteration.

Copyright © 2012 Jon Durbin. See LICENSE.txt for further details.