https://github.com/hopsoft/legion
Parallel processing made easy
https://github.com/hopsoft/legion
Last synced: about 2 months ago
JSON representation
Parallel processing made easy
- Host: GitHub
- URL: https://github.com/hopsoft/legion
- Owner: hopsoft
- License: mit
- Created: 2013-08-05T15:59:52.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2017-08-03T17:14:39.000Z (about 8 years ago)
- Last Synced: 2025-04-18T11:43:52.376Z (6 months ago)
- Language: Ruby
- Homepage:
- Size: 123 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
[](https://codeclimate.com/github/hopsoft/legion)
[](https://gemnasium.com/hopsoft/legion)# 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.
```ruby
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.
```ruby
supervisor = Legion::Supervisor.new(Worker, processes: 7, port: 42042)
supervisor.start1000.times do |i|
supervisor.work # the supervisor asynchronously delegates to the worker
endsupervisor.stop
```## Demo
The demo runs the code in [this file](https://github.com/hopsoft/legion/blob/master/bin/legion_demo).
```
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::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
### 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.
The supervisor delegates work to the first idle Legion::Object it identifies.
If all objects are busy, the supervisor will block & wait.