Project

fork

0.05
No commit activity in last 3 years
No release in over 3 years
Represents forks (child processes) as objects and makes interaction with forks easy. It provides a simple interface to create forked futures, get the return value of the fork, get an exception raised in the fork, and to send objects between parent and forked process.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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.