Skip to content
xchan.rb is a small and easy to use library for sharing Ruby objects between Ruby processes who have a parent-child relationship.
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.gitignore
.travis.yml
.yardopts
Gemfile
LICENSE.txt
README.md
Rakefile
xchan.gemspec

README.md

xchan.rb

  1. Introduction
  2. Examples
  3. Requirements
  4. Install
  5. License
  6. Changelog

Introduction

xchan.rb is a small and easy to use library for sharing Ruby objects between Ruby processes who have a parent-child relationship. It is implemented by serializing a Ruby object and then writing the serialized data to a unix socket. On the other side of the unix socket, in another process the serialized data is transformed back to a Ruby object. The project name xchan.rb comes from an abbreviation of "UNIX Channel".

Examples

1.

The examples mostly explain themselves because they are simple. The first argument given to XChan.from_unix_socket is a serializer, it is a required argument, and it can be any object that implements the dump and load methods. If you are unsure about what serializer to use, use Marshal, because it can serialize the most Ruby objects.

ch = XChan.from_unix_socket Marshal
Process.wait fork { ch.send "Hi parent" }
puts ch.recv
Process.wait fork { ch.send "Bye parent" }
puts ch.recv
ch.close

2.

The second example is similar to the first except it uses JSON to serialize objects. You could also use YAML or MessagePack as serializers.

require 'json'
ch = XChan.from_unix_socket JSON
Process.wait fork { ch.send "Hi parent" }
puts ch.recv
ch.close

3.

The third example sends a message from the parent process to the child process, unlike the other examples that have sent a message from the child process to the parent process.

ch = XChan.from_unix_socket Marshal
pid = fork { puts ch.recv }
ch.send "Hi child"
ch.close
Process.wait(pid)

4.

The fourth example demos how messages are queued until read.

ch = XChan.from_unix_socket Marshal
ch.send 'h'
ch.send 'i'
Process.wait fork {
  msg = ''
  msg << ch.recv
  msg << ch.recv
  puts msg
}
ch.close

Requirements

xchan.rb is light, with 0 external dependencies outside Ruby's core and standard libraries.

Ruby2 or later is recommended. Earlier versions might work.

Install

Gem:

gem install xchan.rb

Bundler:

gem "xchan.rb", "~> 2.0"

License

This project uses the MIT license, please see LICENSE.txt for details.

Changelog

  • v2.0.1 (unreleased)

    • Rename the project to xchan.rb.
    • Minor improvements to the README.
    • Update the project description in the gemspec.
  • v2.0.0

    • Rename XChan.unix() to XChan.from_unix_socket().
    • Improve README and API documentation.
  • v1.0.0

    • First stable release.
You can’t perform that action at this time.