rg 167e6e2675 | ||
---|---|---|
lib | ||
spec | ||
.gitignore | ||
.travis.yml | ||
.yardopts | ||
Gemfile | ||
LICENSE.txt | ||
Rakefile | ||
README.md | ||
xchan.gemspec |
xchan.rb
Introduction
xchan.rb is a small and easy to use library for sharing Ruby objects between Ruby processes who have a parent-child relationship.
Examples
1.
The first example introduces you to the xchan
method, it is implemented as
Object#xchan
and returns an instance of XChan::UNIXSocket
. The first argument
to xchan
is an object that can serialize Ruby objects, in this case Marshal
,
it could also be YAML
, JSON
, MessagePack
, and any other object that
serializes Ruby objects through the dump
and load
methods:
require 'xchan'
ch = xchan Marshal
Process.wait fork {
ch.send "Hi parent"
ch.send "Bye parent"
}
puts ch.recv
puts ch.recv
ch.close
2.
The next example sends a message from the parent process to the child process, unlike the first example that sent messages from the child process to the parent process:
require 'xchan'
ch = xchan Marshal
pid = fork { puts ch.recv }
ch.send "Hi child"
Process.wait(pid)
ch.close
3.
The last example demonstrates how to send and receive messages within a
0.5 second timeout, using the #send!
and #recv!
methods. If the timeout
is exceeded then XChan::TimeoutError
is raised:
require 'xchan'
ch = xchan Marshal
Process.wait fork {
ch.send! 'Hi parent', 0.5
}
ch.recv! 0.5
ch.close
Install
Rubygems:
gem install xchan.rb
Bundler:
gem "xchan.rb", "~> 0.1.0"
License
The MIT license, check out LICENSE.txt for details.