barney 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/barney/share.rb +83 -0
- data/lib/barney.rb +8 -0
- metadata +67 -0
data/lib/barney/share.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module Barney
|
2
|
+
|
3
|
+
class Share
|
4
|
+
|
5
|
+
@mutex = Mutex.new
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# Serves as a temporary holder for the latest value read from the child process.
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @return [void]
|
12
|
+
attr_accessor :value
|
13
|
+
|
14
|
+
# Serves as a lock when {Barney::Share.value} is being accessed by {Barney::Share#synchronize}
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
# @return [Mutex]
|
18
|
+
def mutex
|
19
|
+
@mutex
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Barney::Share] Returns an instance of Barney::Share.
|
24
|
+
def initialize
|
25
|
+
@shared = []
|
26
|
+
@communicators = nil
|
27
|
+
@context = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
# The share method marks a variable or constant to be shared between two processes.
|
31
|
+
#
|
32
|
+
# @param [Symbol] Variable Accepts a variable amount of Symbol objects.
|
33
|
+
# @return [Array<Symbol>] Returns a list of all variables that are being shared.
|
34
|
+
def share(*variables)
|
35
|
+
@shared = @shared | variables
|
36
|
+
end
|
37
|
+
|
38
|
+
# This method will spawn a new child process.
|
39
|
+
# It can be treated like the Kernel.fork method, but a block or Proc object is a
|
40
|
+
# required argument.
|
41
|
+
#
|
42
|
+
# @param [Proc] Proc Accepts a block or Proc object that will be executed in a child
|
43
|
+
# process.
|
44
|
+
#
|
45
|
+
# @raise [ArgumentError] It will raise an ArgumentError if a block or Proc object isn't
|
46
|
+
# supplied as an argument.
|
47
|
+
#
|
48
|
+
# @return [Fixnum] Returns the Process ID(PID) of the spawned child process.
|
49
|
+
def fork(&blk)
|
50
|
+
raise(ArgumentError, "A block or Proc object is expected") unless block_given?
|
51
|
+
|
52
|
+
@communicators = Array.new(@shared.size) { IO.pipe }
|
53
|
+
@context = blk.binding
|
54
|
+
process_id = Kernel.fork do
|
55
|
+
blk.call
|
56
|
+
@communicators.each_with_index do |pipes, i|
|
57
|
+
pipes[0].close
|
58
|
+
pipes[1].write(Marshal.dump(eval("#{@shared[i]}", @context)))
|
59
|
+
pipes[1].close
|
60
|
+
end
|
61
|
+
end
|
62
|
+
process_id
|
63
|
+
end
|
64
|
+
|
65
|
+
# This method synchronizes data between the parent and child process.
|
66
|
+
# This method will block until the spawned child process has exited.
|
67
|
+
# @return [void]
|
68
|
+
def synchronize
|
69
|
+
@communicators.each_with_index do |pipes,i|
|
70
|
+
Barney::Share.mutex.synchronize do
|
71
|
+
pipes[1].close
|
72
|
+
Barney::Share.value = Marshal.load(pipes[0].read)
|
73
|
+
pipes[0].close
|
74
|
+
eval("#{@shared[i]} = Barney::Share.value", @context)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
|
data/lib/barney.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barney
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Robert Gleeson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-04 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Barney tries to make the sharing of data between processes as easy and natural as possible.
|
22
|
+
email: rob@flowof.info
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/barney.rb
|
31
|
+
- lib/barney/share.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/robgleeson/barney
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 1
|
56
|
+
- 3
|
57
|
+
- 6
|
58
|
+
version: 1.3.6
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: "[none]"
|
62
|
+
rubygems_version: 1.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Barney tries to make the sharing of data between processes as easy and natural as possible.
|
66
|
+
test_files: []
|
67
|
+
|