A small and easy to use library for sharing Ruby objects between Ruby processes who have a parent-child relationship.
Go to file
2019-12-29 15:22:49 +01:00
lib v0.1.0 2019-12-29 15:17:22 +01:00
spec v0.1.0 2019-12-29 15:17:22 +01:00
.gitignore update .gitignore 2016-08-12 12:25:26 +01:00
.travis.yml Add Ruby v2.7 to build matrix 2019-12-29 15:21:52 +01:00
.yardopts v0.1.0 2019-12-29 15:17:22 +01:00
Gemfile v0.1.0 2019-12-29 15:17:22 +01:00
LICENSE.txt v0.1.0 2019-12-29 15:17:22 +01:00
Rakefile v0.1.0 2019-12-29 15:17:22 +01:00
README.md README: close channel in last example 2019-12-29 15:22:49 +01:00
xchan.gemspec Rename the project to xchan.rb 2019-08-28 19:07:57 +02:00

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.