# \[ANN\] slave-1.2.1

**URL:** <https://rubytalk.org/t/ann-slave-1-2-1/37232>\
**Category:** ruby-talk\
**Created:** [27 April 2007 22:42 UTC](https://rubytalk.org/t/ann-slave-1-2-1/37232 "2007-04-27T22:42:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [27 April 2007 22:42 UTC](https://rubytalk.org/t/ann-slave-1-2-1/37232/1 "2007-04-27T22:42:23Z")

</div>

SYNOPSIS

&nbsp;&nbsp;&nbsp;the Slave class forks a process and starts a drb server in the child using  
&nbsp;&nbsp;&nbsp;any object as the server. the process is detached so it is not required  
&nbsp;&nbsp;&nbsp;(nor possible) to wait on the child pid. a Heartbeat is set up between the  
&nbsp;&nbsp;&nbsp;parent and child processes so that the child will exit of the parent exits  
&nbsp;&nbsp;&nbsp;for any reason - preventing orphaned slaves from running indefinitely. the  
&nbsp;&nbsp;&nbsp;purpose of Slaves is to be able to easily set up a collection of objects  
&nbsp;&nbsp;&nbsp;communicating via drb protocols instead of having to use IPC.

&nbsp;&nbsp;&nbsp;typical usage:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave::new{ AnyObject.new }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave.object #=\> handle on drb object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave.uri #=\> uri of the drb object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave.socket #=\> unix domain socket path for drb object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave.psname #=\> title shown in ps/top

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object = slave.object

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = object.any\_method #=\> use the object normally

&nbsp;&nbsp;&nbsp;slaves may be configured via the environment, the Slave class, or via the  
&nbsp;&nbsp;&nbsp;ctor for object itself. attributes which may be configured include

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* object : specify the slave object. otherwise block value is used.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* socket\_creation\_attempts : specify how many attempts to create a unix domain socket will be made  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* debug : turn on some logging to STDERR  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* psname : specify the name that will appear in 'top' ($0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* at\_exit : specify a lambda to be called in the \*parent\* when the child dies  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* dumped : specify that the slave object should \*not\* be DRbUndumped (default is DRbUndumped)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* threadsafe : wrap the slave object with ThreadSafe to implement gross thread safety

URIS

&nbsp;&nbsp;&nbsp;[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
&nbsp;&nbsp;&nbsp;[http://codeforpeople.com/lib/ruby/slave](http://codeforpeople.com/lib/ruby/slave)

HISTORY

&nbsp;&nbsp;&nbsp;1.2.1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- jruby/ThreadSafe patches from skaar and ez. using slave.rb with jruby,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;how cool is that!?

SAMPLES

&nbsp;&nbsp;&nbsp;\<========\< samples/a.rb \>========\>

&nbsp;&nbsp;&nbsp;~ \> cat samples/a.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'

> **···**
>
> #  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# simple usage is simply to stand up a server object as a slave. you do not  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# need to wait for the server, join it, etc. it will die when the parent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# process dies - even under 'kill -9' conditions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Server  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def add\_two n  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n + 2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new :object =\> Server.new
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server = slave.object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p server.add\_two(40) #=\> 42
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave.shutdown
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/a.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/b.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/b.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if certain operations need to take place in the child only a block can be  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# used  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Server  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def connect\_to\_db  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"we only want to do this in the child process!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@connection = :postgresql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr :connection  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new('object' =\> Server.new){|s| s.connect\_to\_db}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server = slave.object
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p server.connection #=\> :postgresql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# errors in the child are detected and raised in the parent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new('object' =\> Server.new){|s| s.typo} #=\> raises an error!
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/b.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:postgresql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;samples/b.rb:22: undefined method `typo' for #\<Server:0xb756ed18\> (NoMethodError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./lib/slave.rb:369:in `[]'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./lib/slave.rb:369:in `initialize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from samples/b.rb:22:in `new'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from samples/b.rb:22
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/c.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/c.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if no slave object is given the block itself is used to contruct it  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Server  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"this is run only in the child"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pid = Process.pid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr 'pid'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new{ Server.new }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server = slave.object
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p Process.pid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p server.pid # not going to be the same as parents!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# errors are still detected though  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new{ fubar } # raises error in parent
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/c.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7971  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7972  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;samples/c.rb:21: undefined local variable or method `fubar' for main:Object (NameError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./lib/slave.rb:361:in `call'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from ./lib/slave.rb:361:in `initialize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from samples/c.rb:21:in `new'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from samples/c.rb:21
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/d.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/d.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# at\_exit hanlders are handled correctly in both child and parent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at\_exit{ p 'parent' }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new{ at\_exit{ p 'child' }; 'the server is this string' }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this will print 'child', then 'parent'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/d.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"child"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"parent"
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/e.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/e.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# slaves never outlive their parent. if the parent exits, even under kill -9,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# the child will die.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave = Slave.new{ at\_exit{ p 'child' }; 'the server is this string' }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Process.kill brutal=9, the\_parent\_pid=Process.pid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# even though parent dies a nasty death the child will still print 'child'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/e.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"child"
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/f.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/f.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# slaves created previously are visible to newly created slaves - in this  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# example the child process of slave\_a communicates directly with the child  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# process of slave\_a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave\_a = Slave.new{ Array.new }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slave\_b = Slave.new{ slave\_a.object }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a, b = slave\_b.object, slave\_a.object
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b \<\< 42  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts a #=\> 42
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/f.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42
> 
> &nbsp;&nbsp;&nbsp;\<========\< samples/g.rb \>========\>
> 
> &nbsp;&nbsp;&nbsp;~ \> cat samples/g.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'slave'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Slave.object can used when you want to construct an object in another  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# process. in otherwords you want to fork a process and retrieve a single  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# returned object from that process as opposed to setting up a server.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this = Process.pid  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that = Slave.object{ Process.pid }
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p 'this' =\> this, 'that' =\> that
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# any object can be returned and it can be returned asychronously via a thread  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread = Slave.object(:async =\> true){ sleep 2 and [Process.pid, Time.now] }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this = [Process.pid, Time.now]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that = thread.value
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p 'this' =\> this, 'that' =\> that
> 
> &nbsp;&nbsp;&nbsp;~ \> ruby samples/g.rb
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"that"=\>7990, "this"=\>7989}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"that"=\>[7991, Fri, Apr 27 2007 16:38:30 -0600], "this"=\>[7989, Fri, Apr 27 2007 16:38:28 -0600]}
> 
> enjoy.
> 
> -a  
> --  
> be kind whenever possible... it is always possible.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![John\_Stoffel](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@John\_Stoffel](https://rubytalk.org/u/John_Stoffel)\
**Post date:** [17 February 2009 16:29 UTC](https://rubytalk.org/t/ann-slave-1-2-1/37232/2 "2009-02-17T16:29:38Z")

</div>

Ara.T.Howard wrote:

> SYNOPSIS
> 
> &nbsp;&nbsp;&nbsp;the Slave class forks a process and starts a drb server in the child  
> using  
> &nbsp;&nbsp;&nbsp;any object as the server. the process is detached so it is not  
> required  
> &nbsp;&nbsp;&nbsp;(nor possible) to wait on the child pid. a Heartbeat is set up  
> between the  
> &nbsp;&nbsp;&nbsp;parent and child processes so that the child will exit of the parent  
> exits  
> &nbsp;&nbsp;&nbsp;for any reason - preventing orphaned slaves from running  
> indefinitely. the  
> &nbsp;&nbsp;&nbsp;purpose of Slaves is to be able to easily set up a collection of  
> objects  
> &nbsp;&nbsp;&nbsp;communicating via drb protocols instead of having to use IPC.

Is this being maintained any more? Should I be using some other module  
for the same functionality in the future? It's a nice tool to use, but  
I'm going insane trying to debug issues and warnings with the code in my  
Recursive Parallel directory descent tool.

Thanks,  
John

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
