barney 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +8 -0
- data/README.md +53 -0
- data/lib/barney/share.rb +41 -31
- data/lib/barney.rb +1 -1
- metadata +45 -5
data/ChangeLog
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
![Barney Picture](http://ompldr.org/vNnUwNA)
|
2
|
+
|
3
|
+
Barney tries to make the sharing of data between processes as easy and **natural** as possible.
|
4
|
+
|
5
|
+
## Sharable objects
|
6
|
+
Behind the scenes, Barney is using Marshal to send data between processes.
|
7
|
+
Any object that can be dumped through Marshal.dump can be shared.
|
8
|
+
This excludes anonymous modules, anonymous classes, Proc objects, and possibly some other objects I
|
9
|
+
cannot think of.
|
10
|
+
|
11
|
+
## Thread safety
|
12
|
+
|
13
|
+
Barney is thread-safe as long as one instance of Barney::Share is used per-thread.
|
14
|
+
There is a mutex lock in place, but it only concerns Barney::Share#synchronize, where data is shared
|
15
|
+
among all instances of Barney::Share.
|
16
|
+
|
17
|
+
## Examples
|
18
|
+
|
19
|
+
Okay, now that we've got that out of the way, let's see what using Barney is like.
|
20
|
+
|
21
|
+
**Example A**
|
22
|
+
|
23
|
+
#!/usr/bin/env ruby
|
24
|
+
require 'barney'
|
25
|
+
|
26
|
+
message = 'foo'
|
27
|
+
$times = 2
|
28
|
+
|
29
|
+
obj = Barney::Share.new
|
30
|
+
obj.share :message, :$times
|
31
|
+
pid = obj.fork do
|
32
|
+
message.sub! 'f', 'b'
|
33
|
+
$times += 1
|
34
|
+
end
|
35
|
+
|
36
|
+
Process.wait pid
|
37
|
+
obj.sync
|
38
|
+
|
39
|
+
puts message * $times # output is 'boobooboo'.
|
40
|
+
|
41
|
+
### Documentation
|
42
|
+
|
43
|
+
**API**
|
44
|
+
|
45
|
+
* [0.2.0](http://rubydoc.info/gems/barney/0.2.0)
|
46
|
+
* [0.1.0](http://rubydoc.info/gems/barney/0.1.0)
|
47
|
+
|
48
|
+
### Install
|
49
|
+
|
50
|
+
gem install barney
|
51
|
+
|
52
|
+
The repository has a gemspec you can build and install from, too.
|
53
|
+
I'm following the [Semantic Versioning](http://www.semver.org) policy.
|
data/lib/barney/share.rb
CHANGED
@@ -6,39 +6,49 @@ module Barney
|
|
6
6
|
|
7
7
|
class << self
|
8
8
|
# Serves as a temporary holder for the latest value read from the child process.
|
9
|
-
#
|
10
|
-
# @api private
|
9
|
+
# @api private
|
11
10
|
# @return [void]
|
12
11
|
attr_accessor :value
|
13
12
|
|
14
|
-
# Serves as a lock when {Barney::Share.value} is being accessed by {Barney::Share#synchronize}
|
15
|
-
#
|
16
|
-
# @api private
|
13
|
+
# Serves as a lock when {Barney::Share.value Barney::Share.value} is being accessed by {Barney::Share#synchronize}
|
14
|
+
# @api private
|
17
15
|
# @return [Mutex]
|
18
|
-
|
19
|
-
@mutex
|
20
|
-
end
|
16
|
+
attr_reader :mutex
|
21
17
|
end
|
22
18
|
|
19
|
+
# Returns a list of all variables or constants being shared for this instance of {Barney::Share Barney::Share}.
|
20
|
+
# @return [Array<Symbol>]
|
21
|
+
attr_reader :shared
|
22
|
+
def shared; @shared.keys; end
|
23
|
+
|
23
24
|
# @return [Barney::Share] Returns an instance of Barney::Share.
|
24
25
|
def initialize
|
25
|
-
@shared
|
26
|
-
@
|
27
|
-
@context = nil
|
26
|
+
@shared = Hash.new
|
27
|
+
@context = nil
|
28
28
|
end
|
29
29
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# @
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
# Serves as a method to mark a variable or constant to be shared between two processes.
|
31
|
+
# @param [Symbol] Variable Accepts a variable amount of Symbol objects.
|
32
|
+
# @return [Array<Symbol>] Returns a list of all variables that are being shared.
|
33
|
+
def share *variables
|
34
|
+
variables.map(&:to_sym).each do |variable|
|
35
|
+
@shared.store variable, IO.pipe
|
36
|
+
end
|
37
|
+
@shared.keys
|
36
38
|
end
|
37
39
|
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
40
|
+
# Serves as a method to remove a variable or constant from being shared between two processes.
|
41
|
+
# @param [Symbol] Variable Accepts a variable amount of Symbol objects.
|
42
|
+
# @return [Array<Symbol>] Returns a list of the variables that are still being shared.
|
43
|
+
def unshare *variables
|
44
|
+
variables.map(&:to_sym).each do |variable|
|
45
|
+
@shared.delete variable
|
46
|
+
end
|
47
|
+
@shared.keys
|
48
|
+
end
|
49
|
+
|
50
|
+
# Serves as a method to spawn a new child process.
|
51
|
+
# It can be treated like the Kernel.fork method, but a block or Proc object is a required argument.
|
42
52
|
# @param [Proc] Proc Accepts a block or Proc object that will be executed in a child
|
43
53
|
# process.
|
44
54
|
#
|
@@ -46,35 +56,35 @@ module Barney
|
|
46
56
|
# supplied as an argument.
|
47
57
|
#
|
48
58
|
# @return [Fixnum] Returns the Process ID(PID) of the spawned child process.
|
49
|
-
def fork
|
50
|
-
raise
|
59
|
+
def fork &blk
|
60
|
+
raise ArgumentError, "A block or Proc object is expected" unless block_given?
|
51
61
|
|
52
|
-
@communicators = Array.new(@shared.size) { IO.pipe }
|
53
62
|
@context = blk.binding
|
54
63
|
process_id = Kernel.fork do
|
55
64
|
blk.call
|
56
|
-
@
|
65
|
+
@shared.each do |variable, pipes|
|
57
66
|
pipes[0].close
|
58
|
-
pipes[1].write
|
67
|
+
pipes[1].write Marshal.dump(eval("#{variable}", @context))
|
59
68
|
pipes[1].close
|
60
69
|
end
|
61
70
|
end
|
62
71
|
process_id
|
63
72
|
end
|
64
73
|
|
65
|
-
#
|
66
|
-
#
|
74
|
+
# Serves as a method that synchronizes data between the parent and child process.
|
75
|
+
# It will block until the spawned child process has exited.
|
67
76
|
# @return [void]
|
68
77
|
def synchronize
|
69
|
-
@
|
78
|
+
@shared.each do |variable, pipes|
|
70
79
|
Barney::Share.mutex.synchronize do
|
71
80
|
pipes[1].close
|
72
|
-
Barney::Share.value = Marshal.load
|
81
|
+
Barney::Share.value = Marshal.load pipes[0].read
|
73
82
|
pipes[0].close
|
74
|
-
eval
|
83
|
+
eval "#{variable} = Barney::Share.value", @context
|
75
84
|
end
|
76
85
|
end
|
77
86
|
end
|
87
|
+
alias_method :sync, :synchronize
|
78
88
|
|
79
89
|
end
|
80
90
|
|
data/lib/barney.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Robert Gleeson
|
@@ -14,10 +14,48 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-04 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: yard
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bluecloth
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: riot
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id003
|
21
59
|
description: Barney tries to make the sharing of data between processes as easy and natural as possible.
|
22
60
|
email: rob@flowof.info
|
23
61
|
executables: []
|
@@ -27,6 +65,8 @@ extensions: []
|
|
27
65
|
extra_rdoc_files: []
|
28
66
|
|
29
67
|
files:
|
68
|
+
- README.md
|
69
|
+
- ChangeLog
|
30
70
|
- lib/barney.rb
|
31
71
|
- lib/barney/share.rb
|
32
72
|
has_rdoc: true
|