barney 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/.yardopts +9 -0
  2. data/ChangeLog +5 -1
  3. data/LICENSE +166 -0
  4. data/README.md +56 -2
  5. data/lib/barney/share.rb +23 -11
  6. data/lib/barney.rb +1 -1
  7. metadata +16 -6
data/.yardopts ADDED
@@ -0,0 +1,9 @@
1
+ -m markdown
2
+ --no-private
3
+ --readme README.md
4
+ --hide-void-return
5
+ --no-cache
6
+ -
7
+ LICENSE
8
+ ChangeLog
9
+
data/ChangeLog CHANGED
@@ -1,7 +1,11 @@
1
+ 2011-02-12 Robert Gleeson <rob@flowof.info> v0.4.0
2
+
3
+ * Add Barney::Share#history.
4
+
1
5
  2011-01-16 Robert Gleeson <rob@flowof.info> v0.3.1
2
6
 
3
7
  * Bug Fix(See GH-Issue 1)
4
- * Process ID is available at Barney::Share#pid.
8
+ * Add Barney::Share#pid.
5
9
 
6
10
  2011-01-04 Robert Gleeson <rob@flowof.info> v0.2.0
7
11
 
data/LICENSE ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  ![Barney Picture](http://ompldr.org/vNnUwNA)
2
2
 
3
3
  Barney tries to make the sharing of data between processes as easy and **natural** as possible.
4
+ Barney is developed against Ruby 1.9.1 or later. I'm not knowingly supporting earlier versions of Ruby.
4
5
 
5
6
  ## Sharable objects
6
7
  Behind the scenes, Barney is using Marshal to send data between processes.
@@ -18,7 +19,7 @@ among all instances of Barney::Share.
18
19
 
19
20
  Okay, now that we've got that out of the way, let's see what using Barney is like.
20
21
 
21
- **Example A**
22
+ **Basic**
22
23
 
23
24
  #!/usr/bin/env ruby
24
25
  require 'barney'
@@ -37,15 +38,68 @@ Okay, now that we've got that out of the way, let's see what using Barney is lik
37
38
  obj.sync
38
39
 
39
40
  puts message * $times # output is 'boobooboo'.
40
-
41
+
42
+ **Sequential Jobs**
43
+
44
+ #!/usr/bin/env ruby
45
+ require 'barney'
46
+
47
+ a = "12"
48
+
49
+ obj = Barney::Share.new
50
+ obj.share :a
51
+
52
+ 3.upto(4).each do |i|
53
+ pid = obj.fork { a << i.to_s }
54
+ obj.sync
55
+ Process.wait pid
56
+ end
57
+
58
+ a # => "1234"
59
+
60
+ **Paralell Jobs**
61
+
62
+ #/usr/bin/env ruby
63
+ require 'barney'
64
+
65
+ results = {}
66
+ pids = []
67
+
68
+ obj = Barney::Share.new
69
+ obj.share :results
70
+
71
+ [1,2,3].each do |e|
72
+ pids << obj.fork do
73
+ results.merge!(e => e)
74
+ end
75
+ end
76
+
77
+ pids.each { |pid| Process.wait(pid) }
78
+ obj.sync
79
+
80
+ obj.history.each_value do |history|
81
+ results.merge! history[:results]
82
+ end
83
+
84
+ puts results.inspect # => { 1 => 1, 2 => 2, 3 => 3 }
85
+
41
86
  ## Documentation
42
87
 
43
88
  **API**
44
89
 
45
90
  * [master (git)](http://rubydoc.info/github/robgleeson/Barney/master/)
91
+ * [0.4.0](http://rubydoc.info/gems/barney/0.4.0)
92
+ * [0.3.1](http://rubydoc.info/gems/barney/0.3.1)
46
93
  * [0.2.0](http://rubydoc.info/gems/barney/0.2.0)
47
94
  * [0.1.0](http://rubydoc.info/gems/barney/0.1.0)
48
95
 
96
+ ## License
97
+
98
+ Barney is released under the LGPL.
99
+ You are free to **use** this library in free or commercial software, and license that software however you want,
100
+ but as soon as you modify the library itself,
101
+ you must share your changes under the same license. :)
102
+
49
103
  ## Install
50
104
 
51
105
  gem install barney
data/lib/barney/share.rb CHANGED
@@ -25,10 +25,16 @@ module Barney
25
25
  # @return [Fixnum] Returns the Process ID as a Fixnum.
26
26
  attr_reader :pid
27
27
 
28
+ # Serves as a method to provide a history of changes made in multiple forks for a single instance of {Barney::Share}.
29
+ # @return [{ Fixnum => { Symbol => Object }}] Fixnum represents sequence, Symbol the variable, and Object its value.
30
+ attr_reader :history
31
+
28
32
  # @return [Barney::Share] Returns an instance of Barney::Share.
29
33
  def initialize
30
34
  @shared = Hash.new
31
35
  @context = nil
36
+ @history = {}
37
+ @seq = 0
32
38
  end
33
39
 
34
40
  # Serves as a method to mark a variable or constant to be shared between two processes.
@@ -36,7 +42,7 @@ module Barney
36
42
  # @return [Array<Symbol>] Returns a list of all variables that are being shared.
37
43
  def share *variables
38
44
  variables.map(&:to_sym).each do |variable|
39
- @shared.store variable, IO.pipe
45
+ @shared.store variable, (@shared[variable] || {}).merge({ @seq => IO.pipe })
40
46
  end
41
47
  @shared.keys
42
48
  end
@@ -68,13 +74,14 @@ module Barney
68
74
  @context = blk.binding
69
75
  @pid = Kernel.fork do
70
76
  blk.call
71
- @shared.each do |variable, pipes|
72
- pipes[0].close
73
- pipes[1].write Marshal.dump(eval("#{variable}", @context))
74
- pipes[1].close
77
+ @shared.each do |variable, hash|
78
+ hash[@seq][0].close
79
+ hash[@seq][1].write Marshal.dump(eval("#{variable}", @context))
80
+ hash[@seq][1].close
75
81
  end
76
82
  end
77
-
83
+
84
+ @seq += 1
78
85
  @pid
79
86
  end
80
87
 
@@ -82,12 +89,17 @@ module Barney
82
89
  # It will block until the spawned child process has exited.
83
90
  # @return [void]
84
91
  def synchronize
85
- @shared.each do |variable, pipes|
92
+ @shared.each do |variable, hash|
86
93
  Barney::Share.mutex.synchronize do
87
- pipes[1].close
88
- Barney::Share.value = Marshal.load pipes[0].read
89
- pipes[0].close
90
- eval "#{variable} = Barney::Share.value", @context
94
+ 0.upto(@seq) do |seq|
95
+ unless hash[seq].nil? || hash[seq][0].closed? || hash[seq][1].closed?
96
+ hash[seq][1].close
97
+ Barney::Share.value = Marshal.load hash[seq][0].read
98
+ hash[seq][0].close
99
+ object = eval "#{variable} = Barney::Share.value", @context
100
+ @history.merge!({ seq => { variable => object } })
101
+ end
102
+ end
91
103
  end
92
104
  end
93
105
  end
data/lib/barney.rb CHANGED
@@ -2,7 +2,7 @@ require('barney/share')
2
2
 
3
3
  module Barney
4
4
 
5
- VERSION = '0.3.1'
5
+ VERSION = '0.4.0'
6
6
 
7
7
  end
8
8
 
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barney
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 15
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Robert Gleeson
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-16 00:00:00 +00:00
18
+ date: 2011-02-13 00:00:00 +00:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 3
28
30
  segments:
29
31
  - 0
30
32
  version: "0"
@@ -38,6 +40,7 @@ dependencies:
38
40
  requirements:
39
41
  - - ">="
40
42
  - !ruby/object:Gem::Version
43
+ hash: 3
41
44
  segments:
42
45
  - 0
43
46
  version: "0"
@@ -51,6 +54,7 @@ dependencies:
51
54
  requirements:
52
55
  - - ">="
53
56
  - !ruby/object:Gem::Version
57
+ hash: 3
54
58
  segments:
55
59
  - 0
56
60
  version: "0"
@@ -65,6 +69,8 @@ extensions: []
65
69
  extra_rdoc_files: []
66
70
 
67
71
  files:
72
+ - LICENSE
73
+ - .yardopts
68
74
  - README.md
69
75
  - ChangeLog
70
76
  - lib/barney/share.rb
@@ -83,14 +89,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
89
  requirements:
84
90
  - - ">="
85
91
  - !ruby/object:Gem::Version
92
+ hash: 49
86
93
  segments:
87
- - 0
88
- version: "0"
94
+ - 1
95
+ - 9
96
+ - 1
97
+ version: 1.9.1
89
98
  required_rubygems_version: !ruby/object:Gem::Requirement
90
99
  none: false
91
100
  requirements:
92
101
  - - ">="
93
102
  - !ruby/object:Gem::Version
103
+ hash: 23
94
104
  segments:
95
105
  - 1
96
106
  - 3