Project

legion

0.0
No commit activity in last 3 years
No release in over 3 years
Concurrent processing made easy.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Lines of Code Code Status Dependency Status

Sponsor

Legion

Parallel processing made easy

Legion leverages distibuted Ruby (DRb) & threads to give you concurrency & parallel computing power.

NOTE: Requires Ruby 2+

Quick Start

Create a class that performs a single task.

class Worker < Legion::Object
  # our single task
  # - can be named anything you like
  # - can take as many args as you like
  def work
    # expensive operations & logic
  end

  # runs after the new process has been forked and
  # after DRb has been started (exposing the object as a service)
  after_fork do
    # db reconnect, etc...
  end
end

Use a supervisor to perform work in parallel.

supervisor = Legion::Supervisor.new(Worker, processes: 7, port: 42042)
supervisor.start

1000.times do |i|
  supervisor.work # the supervisor asynchronously delegates to the worker
end

supervisor.stop

Demo

The demo runs the code in this file.

gem install legion
legion_demo

How it Works

Legion::Object

Legion::Objects know how to create remote instances of themselves. They do this by forking the main process then starting a DRb server backed by themselves.

Legion::Object

Legion::Supervisor

Legion::Supervisors wrap a Legion::Object and provide helper methods for managing a cluster of remote objects.

Supervisors perform the following operations.

  • Start & stop the cluster of remote objects
  • Delegate messages (i.e. method calls) to the cluster

Legion::Object

Async Method Invocation

Legion::Objects implicitly create async versions of all defined methods. These async methods delegate to the real method on another thread and return immediately. The Legion::Object is considered busy while the background thread is working.

Legion::Supervisors respond to any methods defined on the wrapped Legion::Object. The distinction being that they asynchronously delegate method calls to the cluster.

Legion::Object

The supervisor delegates work to the first idle Legion::Object it identifies. If all objects are busy, the supervisor will block & wait.