slave 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +16 -11
- data/Rakefile +14 -8
- data/lib/slave.rb +66 -81
- data/{README.tmpl → readme.erb} +4 -1
- data/samples/a.rb +2 -2
- data/slave.gemspec +42 -0
- metadata +8 -31
- data/doc/classes/Slave.html +0 -997
- data/doc/classes/Slave/LifeLine.html +0 -419
- data/doc/classes/Slave/ThreadSafe.html +0 -292
- data/doc/classes/Slave/ThreadSafeHash.html +0 -158
- data/doc/classes/o.html +0 -117
- data/doc/created.rid +0 -1
- data/doc/dot/f_0.dot +0 -14
- data/doc/dot/f_0.jpg +0 -0
- data/doc/dot/f_1.dot +0 -29
- data/doc/dot/f_1.jpg +0 -0
- data/doc/files/README.html +0 -411
- data/doc/files/lib/slave_rb.html +0 -120
- data/doc/fr_class_index.html +0 -30
- data/doc/fr_file_index.html +0 -28
- data/doc/fr_method_index.html +0 -56
- data/doc/index.html +0 -24
- data/doc/rdoc-style.css +0 -208
- data/gemspec.rb +0 -23
- data/gen_readme.rb +0 -32
- data/install.rb +0 -206
- data/rdoc.cmd +0 -1
- data/test.old/slave.rb +0 -21
@@ -1,158 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>Class: Slave::ThreadSafeHash</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="classHeader">
|
50
|
-
<table class="header-table">
|
51
|
-
<tr class="top-aligned-row">
|
52
|
-
<td><strong>Class</strong></td>
|
53
|
-
<td class="class-name-in-header">Slave::ThreadSafeHash</td>
|
54
|
-
</tr>
|
55
|
-
<tr class="top-aligned-row">
|
56
|
-
<td><strong>In:</strong></td>
|
57
|
-
<td>
|
58
|
-
<a href="../../files/lib/slave_rb.html">
|
59
|
-
lib/slave.rb
|
60
|
-
</a>
|
61
|
-
<br />
|
62
|
-
</td>
|
63
|
-
</tr>
|
64
|
-
|
65
|
-
<tr class="top-aligned-row">
|
66
|
-
<td><strong>Parent:</strong></td>
|
67
|
-
<td>
|
68
|
-
Hash
|
69
|
-
</td>
|
70
|
-
</tr>
|
71
|
-
</table>
|
72
|
-
</div>
|
73
|
-
<!-- banner header -->
|
74
|
-
|
75
|
-
<div id="bodyContent">
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
<div id="contextContent">
|
80
|
-
<div id="diagram">
|
81
|
-
<map id="map" name="map">
|
82
|
-
<area shape="rect" coords="27,50,99,98" href="../Slave.html" alt="Slave" />
|
83
|
-
</map>
|
84
|
-
<img src="../../dot/f_1.jpg" usemap="#map" border="0" alt="dot/f_1.jpg">
|
85
|
-
</div>
|
86
|
-
|
87
|
-
<div id="description">
|
88
|
-
<p>
|
89
|
-
a simple thread safe hash used to map object_id to a set of file
|
90
|
-
descriptors in the <a href="LifeLine.html">LifeLine</a> class. see
|
91
|
-
LifeLine::FDS
|
92
|
-
</p>
|
93
|
-
|
94
|
-
</div>
|
95
|
-
|
96
|
-
|
97
|
-
</div>
|
98
|
-
|
99
|
-
<div id="method-list">
|
100
|
-
<h3 class="section-bar">Methods</h3>
|
101
|
-
|
102
|
-
<div class="name-list">
|
103
|
-
<a href="#M000030">new</a>
|
104
|
-
</div>
|
105
|
-
</div>
|
106
|
-
|
107
|
-
</div>
|
108
|
-
|
109
|
-
|
110
|
-
<!-- if includes -->
|
111
|
-
|
112
|
-
<div id="section">
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
<!-- if method_list -->
|
122
|
-
<div id="methods">
|
123
|
-
<h3 class="section-bar">Public Class methods</h3>
|
124
|
-
|
125
|
-
<div id="method-M000030" class="method-detail">
|
126
|
-
<a name="M000030"></a>
|
127
|
-
|
128
|
-
<div class="method-heading">
|
129
|
-
<a href="#M000030" class="method-signature">
|
130
|
-
<span class="method-name">new</span><span class="method-args">(*a, &b)</span>
|
131
|
-
</a>
|
132
|
-
</div>
|
133
|
-
|
134
|
-
<div class="method-description">
|
135
|
-
<p><a class="source-toggle" href="#"
|
136
|
-
onclick="toggleCode('M000030-source');return false;">[Source]</a></p>
|
137
|
-
<div class="method-source-code" id="M000030-source">
|
138
|
-
<pre>
|
139
|
-
<span class="ruby-comment cmt"># File lib/slave.rb, line 156</span>
|
140
|
-
156: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">b</span>) <span class="ruby-constant">ThreadSafe</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">super</span>) <span class="ruby-keyword kw">end</span>
|
141
|
-
</pre>
|
142
|
-
</div>
|
143
|
-
</div>
|
144
|
-
</div>
|
145
|
-
|
146
|
-
|
147
|
-
</div>
|
148
|
-
|
149
|
-
|
150
|
-
</div>
|
151
|
-
|
152
|
-
|
153
|
-
<div id="validator-badges">
|
154
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
155
|
-
</div>
|
156
|
-
|
157
|
-
</body>
|
158
|
-
</html>
|
data/doc/classes/o.html
DELETED
@@ -1,117 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>Class: o</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="classHeader">
|
50
|
-
<h1>o <sup class="type-note">(Class)</sup></h1>
|
51
|
-
<table class="header-table">
|
52
|
-
<tr class="top-aligned-row">
|
53
|
-
<td><strong>In:</strong></td>
|
54
|
-
<td>
|
55
|
-
<a href="../files/lib/slave_rb.html">
|
56
|
-
lib/slave.rb
|
57
|
-
</a>
|
58
|
-
<br />
|
59
|
-
</td>
|
60
|
-
</tr>
|
61
|
-
|
62
|
-
</table>
|
63
|
-
</div>
|
64
|
-
<!-- banner header -->
|
65
|
-
|
66
|
-
<div id="bodyContent">
|
67
|
-
|
68
|
-
|
69
|
-
<div id="contextContent">
|
70
|
-
<div id="diagram">
|
71
|
-
<map name="map">
|
72
|
-
<area shape="RECT" coords="123,98,195,50" href="o.html" alt="o">
|
73
|
-
<area shape="RECT" coords="27,98,99,50" href="Slave.html" alt="Slave">
|
74
|
-
</map>
|
75
|
-
<img src="../dot/f_1.jpg" usemap="#map" border=0 alt="TopLevel">
|
76
|
-
</div>
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
<div id="attribute-list">
|
85
|
-
<h2 class="section-bar">Attributes</h2>
|
86
|
-
|
87
|
-
<div class="name-list">
|
88
|
-
<table>
|
89
|
-
<tr class="top-aligned-row context-row">
|
90
|
-
<td class="context-item-name">__slave_object_failure__</td>
|
91
|
-
<td class="context-item-value"> [RW] </td>
|
92
|
-
<td class="context-item-desc"></td>
|
93
|
-
</tr>
|
94
|
-
</table>
|
95
|
-
</div>
|
96
|
-
</div>
|
97
|
-
|
98
|
-
|
99
|
-
</div>
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
<!-- if includes -->
|
104
|
-
|
105
|
-
|
106
|
-
<!-- if method_list -->
|
107
|
-
|
108
|
-
|
109
|
-
</div>
|
110
|
-
|
111
|
-
|
112
|
-
<div id="validator-badges">
|
113
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
114
|
-
</div>
|
115
|
-
|
116
|
-
</body>
|
117
|
-
</html>
|
data/doc/created.rid
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Fri, 27 Apr 2007 16:38:57 -0600
|
data/doc/dot/f_0.dot
DELETED
data/doc/dot/f_0.jpg
DELETED
Binary file
|
data/doc/dot/f_1.dot
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
digraph TopLevel {
|
2
|
-
compound = true
|
3
|
-
bgcolor = lightcyan1
|
4
|
-
fontname = Arial
|
5
|
-
fontsize = 8
|
6
|
-
label = "TopLevel"
|
7
|
-
node [
|
8
|
-
fontname = Arial,
|
9
|
-
fontsize = 8,
|
10
|
-
color = black
|
11
|
-
]
|
12
|
-
|
13
|
-
subgraph cluster_1 {
|
14
|
-
fontname = Arial
|
15
|
-
color = red
|
16
|
-
label = "lib/slave.rb"
|
17
|
-
Slave [
|
18
|
-
fontcolor = black,
|
19
|
-
URL = "classes/Slave.html",
|
20
|
-
shape = ellipse,
|
21
|
-
color = palegoldenrod,
|
22
|
-
style = filled,
|
23
|
-
label = "Slave"
|
24
|
-
]
|
25
|
-
|
26
|
-
}
|
27
|
-
|
28
|
-
}
|
29
|
-
|
data/doc/dot/f_1.jpg
DELETED
Binary file
|
data/doc/files/README.html
DELETED
@@ -1,411 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>File: README</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="fileHeader">
|
50
|
-
<h1>README</h1>
|
51
|
-
<table class="header-table">
|
52
|
-
<tr class="top-aligned-row">
|
53
|
-
<td><strong>Path:</strong></td>
|
54
|
-
<td>README
|
55
|
-
</td>
|
56
|
-
</tr>
|
57
|
-
<tr class="top-aligned-row">
|
58
|
-
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Fri, Apr 27 2007 16:38:30 -0600</td>
|
60
|
-
</tr>
|
61
|
-
</table>
|
62
|
-
</div>
|
63
|
-
<!-- banner header -->
|
64
|
-
|
65
|
-
<div id="bodyContent">
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
<div id="contextContent">
|
70
|
-
<div id="diagram">
|
71
|
-
<map id="map" name="map">
|
72
|
-
</map>
|
73
|
-
<img src="../dot/f_0.jpg" usemap="#map" border="0" alt="dot/f_0.jpg">
|
74
|
-
</div>
|
75
|
-
|
76
|
-
<div id="description">
|
77
|
-
<p>
|
78
|
-
SYNOPSIS
|
79
|
-
</p>
|
80
|
-
<pre>
|
81
|
-
the Slave class forks a process and starts a drb server in the child using
|
82
|
-
any object as the server. the process is detached so it is not required
|
83
|
-
(nor possible) to wait on the child pid. a Heartbeat is set up between the
|
84
|
-
parent and child processes so that the child will exit of the parent exits
|
85
|
-
for any reason - preventing orphaned slaves from running indefinitely. the
|
86
|
-
purpose of Slaves is to be able to easily set up a collection of objects
|
87
|
-
communicating via drb protocols instead of having to use IPC.
|
88
|
-
|
89
|
-
typical usage:
|
90
|
-
|
91
|
-
slave = Slave::new{ AnyObject.new }
|
92
|
-
|
93
|
-
slave.object #=> handle on drb object
|
94
|
-
slave.uri #=> uri of the drb object
|
95
|
-
slave.socket #=> unix domain socket path for drb object
|
96
|
-
slave.psname #=> title shown in ps/top
|
97
|
-
|
98
|
-
object = slave.object
|
99
|
-
|
100
|
-
value = object.any_method #=> use the object normally
|
101
|
-
|
102
|
-
slaves may be configured via the environment, the Slave class, or via the
|
103
|
-
ctor for object itself. attributes which may be configured include
|
104
|
-
|
105
|
-
* object : specify the slave object. otherwise block value is used.
|
106
|
-
* socket_creation_attempts : specify how many attempts to create a unix domain socket will be made
|
107
|
-
* debug : turn on some logging to STDERR
|
108
|
-
* psname : specify the name that will appear in 'top' ($0)
|
109
|
-
* at_exit : specify a lambda to be called in the *parent* when the child dies
|
110
|
-
* dumped : specify that the slave object should *not* be DRbUndumped (default is DRbUndumped)
|
111
|
-
* threadsafe : wrap the slave object with ThreadSafe to implement gross thread safety
|
112
|
-
</pre>
|
113
|
-
<p>
|
114
|
-
URIS
|
115
|
-
</p>
|
116
|
-
<pre>
|
117
|
-
http://rubyforge.org/projects/codeforpeople/
|
118
|
-
http://codeforpeople.com/lib/ruby/slave
|
119
|
-
</pre>
|
120
|
-
<p>
|
121
|
-
HISTORY
|
122
|
-
</p>
|
123
|
-
<pre>
|
124
|
-
1.2.1:
|
125
|
-
- jruby/ThreadSafe patches from skaar and ez. using slave.rb with jruby,
|
126
|
-
how is that!?
|
127
|
-
|
128
|
-
1.2.0:
|
129
|
-
- cleaned up a bunch of warnings. thanks eric kolve <ekolve@gmail.com>
|
130
|
-
for reporting them.
|
131
|
-
|
132
|
-
1.1.0:
|
133
|
-
- replaced HeartBeat class with LifeLine.
|
134
|
-
|
135
|
-
- __HUGE__ cleanup of file descriptor/fork management with tons of help
|
136
|
-
from skaar and ezra. thanks guys!
|
137
|
-
|
138
|
-
- introduced Slave.object method used to return any object directory from
|
139
|
-
a child process. see samples/g.rb.
|
140
|
-
|
141
|
-
- indroduced keyword to automatically make slave objects threadsafe.
|
142
|
-
remember that your slave object must be threadsafe because they are
|
143
|
-
being server via DRb!!!
|
144
|
-
|
145
|
-
1.0.0:
|
146
|
-
- THIS RELEASE IS !! NOT !! BACKWARD COMPATIBLE. NOTE NEW CTOR SYNTAX.
|
147
|
-
|
148
|
-
- detach method also sets up at_exit handler. extra protection from
|
149
|
-
zombies.
|
150
|
-
|
151
|
-
- ezra zygmuntowicz asked for a feature whereby a parent could be notified
|
152
|
-
when a child exited. obviously such a mechanism should be both async
|
153
|
-
and sync. to accomplish this the wait method was extended to support a
|
154
|
-
callback with is either sync or async
|
155
|
-
|
156
|
-
slave = Server.new{ Server.new }
|
157
|
-
|
158
|
-
slave.wait and puts 'this is sync!'
|
159
|
-
|
160
|
-
slave.wait(:non_block=>true){ 'this is async!' }
|
161
|
-
|
162
|
-
- patch to getval from skaar<skaar@waste.org>. the impl dropped opts
|
163
|
-
delgating to the class method from the instance one.
|
164
|
-
|
165
|
-
0.2.0:
|
166
|
-
incorporated joel vanderWerf's patch such that, if no object is passed the
|
167
|
-
block is used to create one ONLY in the child. this avoids having a copy
|
168
|
-
in both parent and child is that needs to be avoided due to, for instance,
|
169
|
-
resource consumption.
|
170
|
-
|
171
|
-
0.0.1:
|
172
|
-
- patch from Logan Capaldo adds block form to slave new, block is run in the
|
173
|
-
child
|
174
|
-
|
175
|
-
- added a few more samples/*
|
176
|
-
|
177
|
-
- added Slave#wait
|
178
|
-
|
179
|
-
- added status information to slaves
|
180
|
-
|
181
|
-
- added close-on-exec flag to pipes in parent process
|
182
|
-
|
183
|
-
0.0.0:
|
184
|
-
- initial version
|
185
|
-
</pre>
|
186
|
-
<p>
|
187
|
-
SAMPLES
|
188
|
-
</p>
|
189
|
-
<pre>
|
190
|
-
<========< samples/a.rb >========>
|
191
|
-
|
192
|
-
~ > cat samples/a.rb
|
193
|
-
|
194
|
-
require 'slave'
|
195
|
-
#
|
196
|
-
# simple usage is simply to stand up a server object as a slave. you do not
|
197
|
-
# need to wait for the server, join it, etc. it will die when the parent
|
198
|
-
# process dies - even under 'kill -9' conditions
|
199
|
-
#
|
200
|
-
class Server
|
201
|
-
def add_two n
|
202
|
-
n + 2
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
slave = Slave.new :object => Server.new
|
207
|
-
|
208
|
-
server = slave.object
|
209
|
-
p server.add_two(40) #=> 42
|
210
|
-
|
211
|
-
slave.shutdown
|
212
|
-
|
213
|
-
~ > ruby samples/a.rb
|
214
|
-
|
215
|
-
42
|
216
|
-
|
217
|
-
<========< samples/b.rb >========>
|
218
|
-
|
219
|
-
~ > cat samples/b.rb
|
220
|
-
|
221
|
-
require 'slave'
|
222
|
-
#
|
223
|
-
# if certain operations need to take place in the child only a block can be
|
224
|
-
# used
|
225
|
-
#
|
226
|
-
class Server
|
227
|
-
def connect_to_db
|
228
|
-
"we only want to do this in the child process!"
|
229
|
-
@connection = :postgresql
|
230
|
-
end
|
231
|
-
attr :connection
|
232
|
-
end
|
233
|
-
|
234
|
-
slave = Slave.new('object' => Server.new){|s| s.connect_to_db}
|
235
|
-
|
236
|
-
server = slave.object
|
237
|
-
|
238
|
-
p server.connection #=> :postgresql
|
239
|
-
#
|
240
|
-
# errors in the child are detected and raised in the parent
|
241
|
-
#
|
242
|
-
slave = Slave.new('object' => Server.new){|s| s.typo} #=> raises an error!
|
243
|
-
|
244
|
-
~ > ruby samples/b.rb
|
245
|
-
|
246
|
-
:postgresql
|
247
|
-
samples/b.rb:22: undefined method `typo' for #<Server:0xb756ed18> (NoMethodError)
|
248
|
-
from ./lib/slave.rb:369:in `[]'
|
249
|
-
from ./lib/slave.rb:369:in `initialize'
|
250
|
-
from samples/b.rb:22:in `new'
|
251
|
-
from samples/b.rb:22
|
252
|
-
|
253
|
-
<========< samples/c.rb >========>
|
254
|
-
|
255
|
-
~ > cat samples/c.rb
|
256
|
-
|
257
|
-
require 'slave'
|
258
|
-
#
|
259
|
-
# if no slave object is given the block itself is used to contruct it
|
260
|
-
#
|
261
|
-
class Server
|
262
|
-
def initialize
|
263
|
-
"this is run only in the child"
|
264
|
-
@pid = Process.pid
|
265
|
-
end
|
266
|
-
attr 'pid'
|
267
|
-
end
|
268
|
-
|
269
|
-
slave = Slave.new{ Server.new }
|
270
|
-
server = slave.object
|
271
|
-
|
272
|
-
p Process.pid
|
273
|
-
p server.pid # not going to be the same as parents!
|
274
|
-
#
|
275
|
-
# errors are still detected though
|
276
|
-
#
|
277
|
-
slave = Slave.new{ fubar } # raises error in parent
|
278
|
-
|
279
|
-
~ > ruby samples/c.rb
|
280
|
-
|
281
|
-
7971
|
282
|
-
7972
|
283
|
-
samples/c.rb:21: undefined local variable or method `fubar' for main:Object (NameError)
|
284
|
-
from ./lib/slave.rb:361:in `call'
|
285
|
-
from ./lib/slave.rb:361:in `initialize'
|
286
|
-
from samples/c.rb:21:in `new'
|
287
|
-
from samples/c.rb:21
|
288
|
-
|
289
|
-
<========< samples/d.rb >========>
|
290
|
-
|
291
|
-
~ > cat samples/d.rb
|
292
|
-
|
293
|
-
require 'slave'
|
294
|
-
#
|
295
|
-
# at_exit hanlders are handled correctly in both child and parent
|
296
|
-
#
|
297
|
-
at_exit{ p 'parent' }
|
298
|
-
slave = Slave.new{ at_exit{ p 'child' }; 'the server is this string' }
|
299
|
-
#
|
300
|
-
# this will print 'child', then 'parent'
|
301
|
-
#
|
302
|
-
|
303
|
-
~ > ruby samples/d.rb
|
304
|
-
|
305
|
-
"child"
|
306
|
-
"parent"
|
307
|
-
|
308
|
-
<========< samples/e.rb >========>
|
309
|
-
|
310
|
-
~ > cat samples/e.rb
|
311
|
-
|
312
|
-
require 'slave'
|
313
|
-
#
|
314
|
-
# slaves never outlive their parent. if the parent exits, even under kill -9,
|
315
|
-
# the child will die.
|
316
|
-
#
|
317
|
-
slave = Slave.new{ at_exit{ p 'child' }; 'the server is this string' }
|
318
|
-
|
319
|
-
Process.kill brutal=9, the_parent_pid=Process.pid
|
320
|
-
#
|
321
|
-
# even though parent dies a nasty death the child will still print 'child'
|
322
|
-
#
|
323
|
-
|
324
|
-
~ > ruby samples/e.rb
|
325
|
-
|
326
|
-
"child"
|
327
|
-
|
328
|
-
<========< samples/f.rb >========>
|
329
|
-
|
330
|
-
~ > cat samples/f.rb
|
331
|
-
|
332
|
-
require 'slave'
|
333
|
-
#
|
334
|
-
# slaves created previously are visible to newly created slaves - in this
|
335
|
-
# example the child process of slave_a communicates directly with the child
|
336
|
-
# process of slave_a
|
337
|
-
#
|
338
|
-
slave_a = Slave.new{ Array.new }
|
339
|
-
slave_b = Slave.new{ slave_a.object }
|
340
|
-
|
341
|
-
a, b = slave_b.object, slave_a.object
|
342
|
-
|
343
|
-
b << 42
|
344
|
-
puts a #=> 42
|
345
|
-
|
346
|
-
~ > ruby samples/f.rb
|
347
|
-
|
348
|
-
42
|
349
|
-
|
350
|
-
<========< samples/g.rb >========>
|
351
|
-
|
352
|
-
~ > cat samples/g.rb
|
353
|
-
|
354
|
-
require 'slave'
|
355
|
-
#
|
356
|
-
# Slave.object can used when you want to construct an object in another
|
357
|
-
# process. in otherwords you want to fork a process and retrieve a single
|
358
|
-
# returned object from that process as opposed to setting up a server.
|
359
|
-
#
|
360
|
-
this = Process.pid
|
361
|
-
that = Slave.object{ Process.pid }
|
362
|
-
|
363
|
-
p 'this' => this, 'that' => that
|
364
|
-
|
365
|
-
#
|
366
|
-
# any object can be returned and it can be returned asychronously via a thread
|
367
|
-
#
|
368
|
-
thread = Slave.object(:async => true){ sleep 2 and [ Process.pid, Time.now ] }
|
369
|
-
this = [ Process.pid, Time.now ]
|
370
|
-
that = thread.value
|
371
|
-
|
372
|
-
p 'this' => this, 'that' => that
|
373
|
-
|
374
|
-
~ > ruby samples/g.rb
|
375
|
-
|
376
|
-
{"that"=>7990, "this"=>7989}
|
377
|
-
{"that"=>[7991, Fri, Apr 27 2007 16:38:30 -0600], "this"=>[7989, Fri, Apr 27 2007 16:38:28 -0600]}
|
378
|
-
</pre>
|
379
|
-
|
380
|
-
</div>
|
381
|
-
|
382
|
-
|
383
|
-
</div>
|
384
|
-
|
385
|
-
|
386
|
-
</div>
|
387
|
-
|
388
|
-
|
389
|
-
<!-- if includes -->
|
390
|
-
|
391
|
-
<div id="section">
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
<!-- if method_list -->
|
401
|
-
|
402
|
-
|
403
|
-
</div>
|
404
|
-
|
405
|
-
|
406
|
-
<div id="validator-badges">
|
407
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
408
|
-
</div>
|
409
|
-
|
410
|
-
</body>
|
411
|
-
</html>
|