OVERVIEW
Project | IProcess |
---|---|
Homepage | https://github.com/robgleeson/IProcess |
Documentation | http://rubydoc.info/gems/iprocess/frames |
Author | Rob Gleeson |
DESCRIPTION
Provides a number of abstractions on top of spawning subprocesses and interprocess communication. It has a simple and easy to use API that supports synchronous and asynchronous method calls plus one or two other useful features shown in the examples below.
EXAMPLES
The first two examples(one & two) are using the synchronous APIs, a little below those(3) demos the asynchronous API.
1.
Three subprocesses are spawned. The return value of the block, even though executed in a subprocess, is returned to the parent process as long as it may be serialized by Marshal(or the serializer of your choice, this is configurable):
messages = IProcess.spawn(3) { {msg: "hello"} }
p messages # => [{msg: "hello"}, {msg: "hello"}, {msg: "hello"}]
2.
You can spawn a subprocess with a block or with any object that responds to
#call
. If you had a worker that was too complicated as a block you could
try this:
class Worker
def initialize
@num = 1
end
def call
@num + 1
end
end
IProcess.spawn(5, Worker.new) # => [2, 2, 2, 2, 2]
3.
A subprocess is spawned asynchronously.
class Inbox
def initialize
@messages = []
end
def recv(msg)
@messages << msg
end
end
inbox = Inbox.new
jobs = IProcess.spawn! { Process.pid }
jobs.map { |job| job.report_to(inbox) }
SERIALIZERS
A serializer is any object that implements load & dump.
You can choose what serializer you'd like to use through
the IProcess.serializer=
method:
IProcess.serializer = JSON
I know JSON & Marshal(the default) are supported out of the box because they implement both of those methods. MessagePack does not however but you could easily write a wrapper:
require 'msgpack'
obj = Class.new do
def self.dump(obj)
MessagePack.pack(obj)
end
def self.load(obj)
MessagePack.unpack(obj)
end
end
IProcess.serializer = obj
PLATFORM SUPPORT
supported
- Rubinius (1.9 mode)
- CRuby (1.9)
unsupported
- CRuby 1.8
- MacRuby
- JRuby
LICENSE
MIT. See LICENSE.txt.
INSTALL
$ gem install iprocess