team_effort 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/lib/team_effort.rb +12 -5
- data/lib/team_effort/version.rb +1 -1
- data/test/team_effort_test.rb +23 -7
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd78f953296ae896e5e3c7adff6b079b04203376c2cddf7e57a0fdde2c7757ca
|
4
|
+
data.tar.gz: 97bbea07471ca7c8e84a53db5e25ce113f03d8c505bce7c73b6c0327c73f2b53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fce2986ca5a3812d5d2e6af686ae8ab423c5f2c2cf80a55eff218a42fef43257e2cfe5e815bd49b1378f2393185b82b141eed3f76d0d3dfc8af9a8cd5fcc26c
|
7
|
+
data.tar.gz: 309b7f39b82f8cf0e4de1294f6742689c4fc85caa6d28ff904959d2517391a4c1fb4c8670f61a0afe571d0b40f4e3b65d0be1acb26d825fbd45d572f81a46965
|
data/Rakefile
CHANGED
data/lib/team_effort.rb
CHANGED
@@ -4,13 +4,14 @@ module TeamEffort
|
|
4
4
|
def self.work(enumerable, max_process_count = 4, progress_proc: nil)
|
5
5
|
pids = []
|
6
6
|
arg_sets = []
|
7
|
+
previous_proc_result = nil
|
7
8
|
|
8
9
|
max_count = enumerable.count
|
9
10
|
completed_count = 0
|
10
11
|
|
11
12
|
enumerable.each do |arg_set|
|
12
13
|
while pids.size == max_process_count
|
13
|
-
pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
14
|
+
pids, arg_sets, completed_count, previous_proc_result = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
|
14
15
|
end
|
15
16
|
|
16
17
|
pids << fork do
|
@@ -28,11 +29,11 @@ module TeamEffort
|
|
28
29
|
end
|
29
30
|
|
30
31
|
while !pids.empty?
|
31
|
-
pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
32
|
+
pids, arg_sets, completed_count, previous_proc_result = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
def self.wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
36
|
+
def self.wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc, previous_proc_result)
|
36
37
|
pid, status = Process.wait2
|
37
38
|
pids_index = pids.index(pid)
|
38
39
|
if pids_index
|
@@ -40,7 +41,13 @@ module TeamEffort
|
|
40
41
|
arg_set = arg_sets.delete_at pids_index
|
41
42
|
raise "TeamEffort child process failed when processing > #{arg_set} <" if !status.success?
|
42
43
|
end
|
43
|
-
|
44
|
-
|
44
|
+
|
45
|
+
if progress_proc
|
46
|
+
progress_proc_args = [completed_count += 1, max_count]
|
47
|
+
progress_proc_args << previous_proc_result if progress_proc.arity == 3
|
48
|
+
proc_result = progress_proc.call(*progress_proc_args)
|
49
|
+
end
|
50
|
+
|
51
|
+
[pids, arg_sets, completed_count, proc_result]
|
45
52
|
end
|
46
53
|
end
|
data/lib/team_effort/version.rb
CHANGED
data/test/team_effort_test.rb
CHANGED
@@ -24,16 +24,16 @@ describe TeamEffort do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
lines = output.split(/\n/)
|
27
|
-
lines.size.must_equal 3
|
27
|
+
_(lines.size).must_equal 3
|
28
28
|
lines.each do |line|
|
29
|
-
line.must_match(/^\d+$/)
|
29
|
+
_(line).must_match(/^\d+$/)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'exits when a child process fails' do
|
34
34
|
data = [8, 4, 2, 0, 4, 8]
|
35
35
|
|
36
|
-
err = -> {
|
36
|
+
err = _(-> {
|
37
37
|
$stderr = File.open('/dev/null', 'w')
|
38
38
|
begin
|
39
39
|
TeamEffort.work(data, 1) do |item|
|
@@ -43,8 +43,8 @@ describe TeamEffort do
|
|
43
43
|
$stderr.close
|
44
44
|
$stderr = STDERR
|
45
45
|
end
|
46
|
-
}.must_raise RuntimeError
|
47
|
-
err.message.must_match /TeamEffort child process failed when processing > 0 </
|
46
|
+
}).must_raise RuntimeError
|
47
|
+
_(err.message).must_match %r/TeamEffort child process failed when processing > 0 </
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'invokes an optional proc when it completes an item' do
|
@@ -53,13 +53,29 @@ describe TeamEffort do
|
|
53
53
|
proc = ->(item_index, max_items) { proc_data << [item_index, max_items] }
|
54
54
|
TeamEffort.work(data, 1, progress_proc: proc) {}
|
55
55
|
|
56
|
-
proc_data.must_equal [
|
56
|
+
_(proc_data).must_equal [
|
57
57
|
[1, 3],
|
58
58
|
[2, 3],
|
59
59
|
[3, 3],
|
60
60
|
]
|
61
61
|
end
|
62
62
|
|
63
|
+
it 'invokes an optional proc with the previous result of the optional proc when it completes an item' do
|
64
|
+
data = %w|one two three|
|
65
|
+
proc_data = []
|
66
|
+
proc = ->(item_index, max_items, previous_result) {
|
67
|
+
if previous_result == 2
|
68
|
+
proc_data << [item_index, max_items]
|
69
|
+
end
|
70
|
+
item_index
|
71
|
+
}
|
72
|
+
TeamEffort.work(data, 1, progress_proc: proc) {}
|
73
|
+
|
74
|
+
_(proc_data).must_equal [
|
75
|
+
[3, 3]
|
76
|
+
]
|
77
|
+
end
|
78
|
+
|
63
79
|
it 'ignores other child process completions' do
|
64
80
|
output_io_class = Class.new do
|
65
81
|
def initialize
|
@@ -112,7 +128,7 @@ describe TeamEffort do
|
|
112
128
|
|
113
129
|
lines = output_io.lines
|
114
130
|
|
115
|
-
lines.must_equal [
|
131
|
+
_(lines).must_equal [
|
116
132
|
'unmanaged starting',
|
117
133
|
'unmanaged waiting for IO',
|
118
134
|
'task 1 starting',
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: team_effort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Felkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
|
80
|
-
rubygems_version: 2.7.7
|
79
|
+
rubygems_version: 3.0.8
|
81
80
|
signing_key:
|
82
81
|
specification_version: 4
|
83
82
|
summary: Use child processes to process a collection in parallel
|