iprocess 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,10 +12,14 @@ __DESCRIPTION__
12
12
 
13
13
  IProcess, short for _Inter Process Communication(IPC) Process_, is a collection
14
14
  of classes you can use to transport Ruby objects between processes running on
15
- UNIX-like operating systems.
15
+ UNIX-like operating systems.
16
+
16
17
 
17
18
  __EXAMPLES__
18
19
 
20
+ The first two examples(one & two) are using the synchronous APIs, a little below
21
+ those(3) demos the asynchronous API.
22
+
19
23
  __1.__
20
24
 
21
25
  A single subprocess is spawned:
@@ -40,6 +44,19 @@ subprocesses you can spawn is variable (5, in this example):
40
44
 
41
45
  IProcess.spawn(5, Worker.new) # => [2, 2, 2, 2, 2]
42
46
 
47
+ __3.__
48
+
49
+ A single subprocess is spawned asynchronously.
50
+ When the subprocess completes, ':hi' is sent to `obj#recv` from another thread.
51
+
52
+ obj = Object.new
53
+ def obj.recv(msg)
54
+ msg.to_s.captialize!
55
+ end
56
+
57
+ jobs = IProcess.spawn! { :hi }
58
+ jobs.map { |job| job.defer(listener) }
59
+
43
60
 
44
61
  __PLATFORM SUPPORT__
45
62
 
@@ -1,14 +1,11 @@
1
1
  class IProcess::Channel
2
2
 
3
- attr_reader :name
4
-
5
3
  #
6
4
  # @yieldparam [IProcess::Channel] _self
7
5
  # Yields self.
8
6
  #
9
- def initialize name = nil
7
+ def initialize
10
8
  @reader, @writer = IO.pipe
11
- @name = name
12
9
 
13
10
  if block_given?
14
11
  yield self
@@ -65,4 +62,3 @@ class IProcess::Channel
65
62
  end
66
63
 
67
64
  end
68
-
@@ -1,3 +1,3 @@
1
1
  class IProcess
2
- VERSION = '2.0.2'
2
+ VERSION = '2.1.0'
3
3
  end
data/lib/iprocess.rb CHANGED
@@ -1,35 +1,49 @@
1
1
  class IProcess
2
-
3
2
  require_relative 'iprocess/version'
4
3
  require_relative 'iprocess/channel'
5
4
 
6
- #
7
- # @overload spawn(number_of = 1, worker)
8
- #
9
- # Spawn one or more subprocesses.
10
- #
11
- # @param [Integer] number_of
12
- # The number of subprocesses to spawn.
13
- #
14
- # @param [#call] worker
15
- # The unit of work to execute in a subprocess.
16
- #
17
- # @return [Array<Object>]
18
- # The return value of the unit if worker.
19
- #
20
- def self.spawn(number_of = 1, obj = nil, &worker)
21
- worker = obj || worker
5
+ class << self
6
+ #
7
+ # @overload spawn(number_of = 1, worker)
8
+ #
9
+ # Spawn one or more subprocesses.
10
+ #
11
+ # @param [Integer] number_of
12
+ # The number of subprocesses to spawn.
13
+ #
14
+ # @param [#call] worker
15
+ # The unit of work to execute in a subprocess.
16
+ #
17
+ # @return [Array<Object>]
18
+ # The return value of the unit if worker.
19
+ #
20
+ def spawn(*args, &worker)
21
+ fork(*args, &worker).map(&:result)
22
+ end
22
23
 
23
- jobs =
24
- Array.new(number_of) do
25
- job = IProcess.new(worker)
26
- job.execute
27
- job
24
+ #
25
+ # @overload
26
+ #
27
+ # Spawn one or more subprocesses asynchronously.
28
+ #
29
+ # @param
30
+ # (see IProcess.spawn)
31
+ #
32
+ # @return [Array<IProcess>]
33
+ # An array of IProcess objects. See {#defer}.
34
+ #
35
+ def spawn!(*args, &worker)
36
+ fork *args, &worker
28
37
  end
38
+
39
+ def fork(number_of = 1, obj = nil, &worker)
40
+ worker = obj || worker
29
41
 
30
- jobs.map do |job|
31
- job.result
42
+ Array.new(number_of) do
43
+ IProcess.new(worker).tap { |job| job.execute }
44
+ end
32
45
  end
46
+ private :fork
33
47
  end
34
48
 
35
49
  #
@@ -53,6 +67,19 @@ class IProcess
53
67
  end
54
68
  end
55
69
 
70
+ #
71
+ # @param [#recv] listener
72
+ # The listener.
73
+ #
74
+ # @return [void]
75
+ #
76
+ def defer(listener)
77
+ Thread.new do
78
+ Process.wait @pid
79
+ listener.recv @channel.recv
80
+ end
81
+ end
82
+
56
83
  #
57
84
  # Executes a unit of work in a subprocess.
58
85
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iprocess
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000 Z
12
+ date: 2012-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yard
16
- requirement: &12607260 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0.7'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *12607260
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.7'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: redcarpet
27
- requirement: &12606780 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '1.17'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *12606780
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.17'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: minitest
38
- requirement: &12606220 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '2.6'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *12606220
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rake
49
- requirement: &12605720 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: 0.9.2
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *12605720
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2
58
78
  description: A set of classes you can use to transport ruby objects between processes
59
79
  on UNIX-like operating systems.
60
80
  email: rob@flowof.info
@@ -96,12 +116,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
116
  version: 1.3.6
97
117
  requirements: []
98
118
  rubyforge_project: ! '[none]'
99
- rubygems_version: 1.8.11
119
+ rubygems_version: 1.8.23
100
120
  signing_key:
101
121
  specification_version: 3
102
122
  summary: A set of classes you can use to transport ruby objects between processes
103
123
  on UNIX-like operating systems.
104
124
  test_files:
105
- - test/test_IProcess_class.rb
106
125
  - test/setup.rb
126
+ - test/test_IProcess_class.rb
107
127
  has_rdoc: