team_effort 0.0.3 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.ruby-version +1 -1
- data/README.md +11 -14
- data/lib/team_effort.rb +5 -1
- data/lib/team_effort/version.rb +1 -1
- data/test/test_team_effort.rb +13 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 267e445b9850df939f36b9458ed000f4889867bc3c686fb4410b5e6acf2d6f31
|
4
|
+
data.tar.gz: 0a6673448d5e06aa98d7e760c6df4e773008747241e7012a7217c37357e9fcee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d60080c326dd3acbe4e2886d858919e92e01c94e9aa6fa629ffca35829d27868eb674d49bf572fae5bcf9c26939a1ed40047333eb5d122b01b51e864bcfd0b8c
|
7
|
+
data.tar.gz: d1c1a4f3f6ff5fd9b0539df97487d02d5576278c770cab598d26799d81185c61b852f013ada2963167953bf3391f1c675954fc6896d191d923cd73fd24a16b54
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.4.4
|
data/README.md
CHANGED
@@ -51,9 +51,19 @@ You may specify the number of child processes with the work method:
|
|
51
51
|
# do some work on item
|
52
52
|
end
|
53
53
|
```
|
54
|
-
|
55
54
|
The number of child processes defaults to 4.
|
56
55
|
|
56
|
+
You can pass in a proc to receive completion notifications.
|
57
|
+
|
58
|
+
```irb
|
59
|
+
> data = %w|one two three|
|
60
|
+
> progress_proc = ->(index, max_index) { puts "#{ sprintf("%3i%", index.to_f / max_index * 100) }" }
|
61
|
+
> TeamEffort.work(data, 1, progress_proc: progress_proc) {}
|
62
|
+
33%
|
63
|
+
66%
|
64
|
+
100%
|
65
|
+
```
|
66
|
+
|
57
67
|
In rails you need to reestablish your ActiveRecord connection in the
|
58
68
|
child process:
|
59
69
|
|
@@ -72,19 +82,6 @@ child process:
|
|
72
82
|
end
|
73
83
|
```
|
74
84
|
|
75
|
-
Logging can be messy when multiple processes are writing to the same
|
76
|
-
io channel. One approach is to wrap logging statements in a
|
77
|
-
synchronize block:
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
def pid_logger(msg)
|
81
|
-
@mutex ||= Mutex.new
|
82
|
-
@mutex.synchronize do
|
83
|
-
puts "[#{Process.pid} at #{Time.now.strftime('%H:%M:%S')}] #{msg}"
|
84
|
-
end
|
85
|
-
end
|
86
|
-
```
|
87
|
-
|
88
85
|
## Discussion
|
89
86
|
|
90
87
|
TeamEffort uses child processes to do concurrent processing. To review
|
data/lib/team_effort.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
require_relative "team_effort/version"
|
2
2
|
|
3
3
|
module TeamEffort
|
4
|
-
def self.work(enumerable, max_process_count = 4)
|
4
|
+
def self.work(enumerable, max_process_count = 4, progress_proc: nil)
|
5
5
|
pids = []
|
6
|
+
max_count = enumerable.count
|
7
|
+
completed_count = 0
|
6
8
|
|
7
9
|
enumerable.each do |args|
|
8
10
|
while pids.size == max_process_count
|
9
11
|
finished_pid = Process.wait
|
10
12
|
pids.delete finished_pid
|
13
|
+
progress_proc.call(completed_count += 1, max_count) if progress_proc
|
11
14
|
end
|
12
15
|
|
13
16
|
pids << fork do
|
@@ -18,6 +21,7 @@ module TeamEffort
|
|
18
21
|
while !pids.empty?
|
19
22
|
finished_pid = Process.wait
|
20
23
|
pids.delete finished_pid
|
24
|
+
progress_proc.call(completed_count += 1, max_count) if progress_proc
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
data/lib/team_effort/version.rb
CHANGED
data/test/test_team_effort.rb
CHANGED
@@ -30,6 +30,19 @@ describe TeamEffort do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
it 'invokes an optional proc when it completes an item' do
|
34
|
+
data = %w|one two three|
|
35
|
+
proc_data = []
|
36
|
+
# proc = ->(item_index, max_items) {proc_data << [item_index, max_items]}
|
37
|
+
progress_proc = ->(index, max_index) { puts "#{ sprintf("%3i%", index.to_f / max_index * 100) }" }
|
38
|
+
TeamEffort.work(data, 1, progress_proc: progress_proc) {}
|
39
|
+
|
40
|
+
proc_data.must_equal [
|
41
|
+
[1, 3],
|
42
|
+
[2, 3],
|
43
|
+
[3, 3],
|
44
|
+
]
|
45
|
+
end
|
33
46
|
|
34
47
|
it 'ignores other child process completions' do
|
35
48
|
output_io_class = Class.new do
|
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: 0.0.
|
4
|
+
version: 0.0.9
|
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: 2018-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.7.7
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Use child processes to process a collection in parallel
|