Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Represents forks (child processes) as objects and makes interaction with forks easy.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 development
Octocat-spinner-32 doc
Octocat-spinner-32 documentation
Octocat-spinner-32 lib
Octocat-spinner-32 rake
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .yardopts
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 fork.gemspec
README.markdown

README

Summary

Represents forks (child processes) as objects and makes interaction with forks easy.

Features

  • Object oriented usage of forks
  • Easy-to-use implementation of future (Fork.future { computation }.call # => result)
  • Provides facilities for IO between parent and fork
  • Supports sending ruby objects to the forked process
  • Supports reading ruby objects from the forked process

Installation

gem install fork

Usage

An example using a future:

def fib(n) n < 2 ? n : fib(n-1)+fib(n-2); end # <-- bad implementation of fibonacci
future = Fork.future do
  fib(35)
end
# do something expensive in the parent process
puts future.call # this blocks, until the fork finished, and returns the last value

A more complex example, using some of Fork's features:

# Create a fork with two-directional IO, which returns values and raises
# exceptions in the parent process.
fork = Fork.new :to_fork, :from_fork do |fork|
  while received = fork.receive_object
    p :fork_received => received
  end
end
fork.execute # spawn child process and start executing
fork.send_object(123)
puts "Fork runs as process with pid #{fork.pid}"
fork.send_object(nil) # terminate the fork
fork.wait # wait until the fork is indeed terminated
puts "Fork is dead, as expected" if fork.dead?

Links

License

You can use this code under the {file:LICENSE.txt BSD-2-Clause License}, free of charge. If you need a different license, please ask the author.

Something went wrong with that request. Please try again.