iprocess 3.0.2 → 3.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/README.md +16 -8
- data/lib/iprocess/channel.rb +8 -4
- data/lib/iprocess/version.rb +1 -1
- data/lib/iprocess.rb +21 -4
- metadata +2 -2
data/README.md
CHANGED
@@ -10,11 +10,10 @@ __OVERVIEW__
|
|
10
10
|
|
11
11
|
__DESCRIPTION__
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
obvious in the examples below. :-)
|
13
|
+
Provides a number of abstractions on top of spawning subprocesses and interprocess
|
14
|
+
communication. It has a simple and easy to use API that supports synchronous and
|
15
|
+
asynchronous method calls plus one or two other useful features shown in the
|
16
|
+
examples below.
|
18
17
|
|
19
18
|
__EXAMPLES__
|
20
19
|
|
@@ -23,10 +22,12 @@ those(3) demos the asynchronous API.
|
|
23
22
|
|
24
23
|
__1.__
|
25
24
|
|
26
|
-
|
25
|
+
Three subprocesses are spawned. The return value of the block, even though executed
|
26
|
+
in a subprocess, is returned to the parent process as long as it may be serialized
|
27
|
+
by Marshal:
|
27
28
|
|
28
|
-
messages = IProcess.spawn { {msg: "hello"} }
|
29
|
-
p messages # => [{msg: "hello"}]
|
29
|
+
messages = IProcess.spawn(3) { {msg: "hello"} }
|
30
|
+
p messages # => [{msg: "hello"}, {msg: "hello"}, {msg: "hello"}]
|
30
31
|
|
31
32
|
__2.__
|
32
33
|
|
@@ -62,6 +63,13 @@ A subprocess is spawned asynchronously.
|
|
62
63
|
jobs = IProcess.spawn! { Process.pid }
|
63
64
|
jobs.map { |job| job.report_to(inbox) }
|
64
65
|
|
66
|
+
__SERIALIZERS__
|
67
|
+
|
68
|
+
A serializer is any object that implements load & dump.
|
69
|
+
You can choose what serializer you'd like to use through
|
70
|
+
the `IProcess.serializer=` method:
|
71
|
+
|
72
|
+
IProcess.serializer = JSON
|
65
73
|
|
66
74
|
__PLATFORM SUPPORT__
|
67
75
|
|
data/lib/iprocess/channel.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
class IProcess::Channel
|
2
2
|
|
3
|
+
#
|
4
|
+
# @param [#dump,#load}] serializer
|
5
|
+
# Any object that implements dump, & load.
|
6
|
+
# Defaults to {IProcess.serializer}.
|
3
7
|
#
|
4
8
|
# @yieldparam [IProcess::Channel] _self
|
5
9
|
# Yields self.
|
6
10
|
#
|
7
|
-
def initialize
|
11
|
+
def initialize(serializer = IProcess.serializer)
|
8
12
|
@reader, @writer = IO.pipe
|
9
|
-
|
13
|
+
@serializer = serializer
|
10
14
|
if block_given?
|
11
15
|
yield self
|
12
16
|
end
|
@@ -41,7 +45,7 @@ class IProcess::Channel
|
|
41
45
|
def write object
|
42
46
|
if open?
|
43
47
|
@reader.close
|
44
|
-
@writer.write
|
48
|
+
@writer.write @serializer.dump(object)
|
45
49
|
@writer.close
|
46
50
|
end
|
47
51
|
end
|
@@ -55,7 +59,7 @@ class IProcess::Channel
|
|
55
59
|
def recv
|
56
60
|
if open?
|
57
61
|
@writer.close
|
58
|
-
obj =
|
62
|
+
obj = @serializer.load @reader.read
|
59
63
|
@reader.close
|
60
64
|
obj
|
61
65
|
end
|
data/lib/iprocess/version.rb
CHANGED
data/lib/iprocess.rb
CHANGED
@@ -3,6 +3,23 @@ class IProcess
|
|
3
3
|
require_relative 'iprocess/channel'
|
4
4
|
|
5
5
|
class << self
|
6
|
+
#
|
7
|
+
# @return [#load,#dump]
|
8
|
+
# Returns the serializer used by IProcess.
|
9
|
+
#
|
10
|
+
def serializer
|
11
|
+
@serializer || Marshal
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# @param [#load,#dump] obj
|
16
|
+
# Any object that implements #load, & #dump.
|
17
|
+
# Examples could be JSON & Marshal.
|
18
|
+
#
|
19
|
+
def serializer=(obj)
|
20
|
+
@serializer = obj
|
21
|
+
end
|
22
|
+
|
6
23
|
#
|
7
24
|
# @overload spawn(number_of = 1, worker)
|
8
25
|
#
|
@@ -47,10 +64,10 @@ class IProcess
|
|
47
64
|
|
48
65
|
#
|
49
66
|
# @param [#call] worker
|
50
|
-
#
|
67
|
+
# A block or any object that responds to #call.
|
51
68
|
#
|
52
69
|
# @raise [ArgumentError]
|
53
|
-
# If
|
70
|
+
# If 'worker' does not respond to #call.
|
54
71
|
#
|
55
72
|
# @return [IProcess]
|
56
73
|
# Returns self.
|
@@ -58,7 +75,7 @@ class IProcess
|
|
58
75
|
def initialize(worker)
|
59
76
|
@worker = worker
|
60
77
|
@channel = nil
|
61
|
-
@pid
|
78
|
+
@pid = nil
|
62
79
|
unless @worker.respond_to?(:call)
|
63
80
|
raise ArgumentError,
|
64
81
|
"Expected worker to implement #{@worker.class}#call"
|
@@ -67,7 +84,7 @@ class IProcess
|
|
67
84
|
|
68
85
|
#
|
69
86
|
# @param [#recv] obj
|
70
|
-
# An object that will receive
|
87
|
+
# An object that will receive messages.
|
71
88
|
#
|
72
89
|
# @return [void]
|
73
90
|
#
|
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: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yard
|