parallax 0.3.0 → 0.3.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -11
- data/lib/parallax/collectable.rb +2 -1
- data/lib/parallax/version.rb +1 -1
- data/lib/parallax.rb +2 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8550cb7115815b07ee890e22a8d9df2c956b8f7ed4c8d6b3e72cfbcb7045926c
|
4
|
+
data.tar.gz: ab4ce11646317867bc4aca9e6a14b8877255afddc3bb7486831e3522feae7576
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62c77b06eb45b8ddfa0142c962d808ece8a44f41d39d7ec942ab320c59213b32fa83b814dc69b7095e8654b28d7f724046b53830dabd1fb58d9ce6823d00e6ac
|
7
|
+
data.tar.gz: b9def74a52b5ec5132ec7bd26c28aeacecb33cfee4147c6281b5d59f31795cbb476c2f87fc35b89d16bde9be05ea9565848efef2cff1e568100fd76b2cfb074c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -71,21 +71,21 @@ Other options you can pass to execute are:
|
|
71
71
|
* `processes`: the number of processes in which parallelize the execution. Defaults to `Etc.nprocessors` (which is equal to the number of cores of the current running machine).
|
72
72
|
* `collector`: a custom collector object that you can implement yourself.
|
73
73
|
|
74
|
-
To use a custom collector, you need to `include Parallax::Collectable` in your custom collector
|
75
|
-
Example of a custom collector:
|
74
|
+
To use a custom collector, you need to `include Parallax::Collectable` in your custom collector. Example of a custom collector:
|
76
75
|
|
77
76
|
```ruby
|
78
77
|
# custom_collector.rb
|
79
78
|
class CustomCollector
|
80
79
|
include Parallax::Collectable
|
81
80
|
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
attr_accessor :name
|
82
|
+
|
83
|
+
def initialize(name)
|
84
|
+
@name = name
|
85
85
|
end
|
86
86
|
|
87
87
|
def store(worker_index, object)
|
88
|
-
workers_data.push
|
88
|
+
workers_data.push "#{self.name}: worker #{worker_index} stored: #{object}"
|
89
89
|
end
|
90
90
|
end
|
91
91
|
```
|
@@ -94,7 +94,7 @@ end
|
|
94
94
|
workers_count = 4
|
95
95
|
numbers = (0..100).to_a
|
96
96
|
|
97
|
-
custom_collector = CustomCollector.new(
|
97
|
+
custom_collector = CustomCollector.new('custom_collector')
|
98
98
|
Parallax.execute numbers, collector: custom_collector, do |worker, numbers_chunk|
|
99
99
|
numbers_chunk.each do |number|
|
100
100
|
worker.store number * 2
|
@@ -104,10 +104,10 @@ end
|
|
104
104
|
puts custom_collector.workers_data.inspect
|
105
105
|
|
106
106
|
# Example output with 4 cores and custom collector:
|
107
|
-
# [
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
107
|
+
# [ "custom_collector: worker 0 stored 0",
|
108
|
+
# "custom_collector: worker 3 stored 152",
|
109
|
+
# "custom_collector: worker 1 stored 52",
|
110
|
+
# "custom_collector: worker 2 stored 102",
|
111
111
|
# ...
|
112
112
|
```
|
113
113
|
|
data/lib/parallax/collectable.rb
CHANGED
@@ -19,12 +19,13 @@ module Parallax
|
|
19
19
|
#
|
20
20
|
# @param [Integer] workers_count the number of workers running in parallel.
|
21
21
|
#
|
22
|
-
# @return [
|
22
|
+
# @return [Object] the instance of the including object.
|
23
23
|
def initialize_collector(workers_count)
|
24
24
|
@workers_count = workers_count
|
25
25
|
@closed_workers_count = 0
|
26
26
|
@receiving_stream, @sending_stream = IO.pipe
|
27
27
|
@workers_data = []
|
28
|
+
self
|
28
29
|
end
|
29
30
|
|
30
31
|
##
|
data/lib/parallax/version.rb
CHANGED
data/lib/parallax.rb
CHANGED
@@ -9,14 +9,6 @@ module Parallax
|
|
9
9
|
|
10
10
|
class << self
|
11
11
|
|
12
|
-
##
|
13
|
-
# Get the default number of workers.
|
14
|
-
#
|
15
|
-
# @return [Integer] the workers count.
|
16
|
-
def workers_count
|
17
|
-
Etc.nprocessors
|
18
|
-
end
|
19
|
-
|
20
12
|
##
|
21
13
|
# Divides the given elements in groups of N and executes
|
22
14
|
# each chunk in parallel with the given block.
|
@@ -26,10 +18,10 @@ module Parallax
|
|
26
18
|
#
|
27
19
|
# @return [Collector] all processes output collector.
|
28
20
|
def execute(elements, options = {}, &block)
|
29
|
-
processes = options[:processes] ||
|
21
|
+
processes = options[:processes] || Etc.nprocessors
|
30
22
|
|
31
23
|
if options[:collector].present?
|
32
|
-
collector = options[:collector]
|
24
|
+
collector = options[:collector].initialize_collector(processes)
|
33
25
|
else
|
34
26
|
collector = Parallax::Collector.new(processes)
|
35
27
|
end
|