Class: SizedParallel
- Inherits:
-
Object
- Object
- SizedParallel
- Defined in:
- lib/sized_parallel.rb
Overview
SizedParallel is a make -j4 -like experience. When you do
sp = SizedParallel.new
1024.times do |i|
sp.start { printf("1: %d\n", i); sleep 1 }
.then { printf("2: %d\n", i); sleep 1 }
.then { printf("3: %d\n", i); sleep 1 }
end
sp.wait
You see bunch of things run in parallel, but all the 2s are after corresponding 1s, and 3s are after 2s.
Defined Under Namespace
Constant Summary collapse
- VERSION =
Copyright (c) 2015 Urabe, Shyouhei
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'1.0.1'
Instance Method Summary collapse
-
#initialize(n = Etc.nprocessors) {|self| ... } ⇒ SizedParallel
constructor
Creates a set of execution.
-
#start(*argv) {|*argv| ... } ⇒ Task
Starts a series of execution.
-
#wait ⇒ SizedParallel
Waits for tasks.
Constructor Details
#initialize(n = Etc.nprocessors) {|self| ... } ⇒ SizedParallel
Creates a set of execution. If a block is given, evaluates that block and wait for self, i.e:
SizedParallel.new do |sp|
...
end
is a shorthand for:
sp = SizedParallel.new
begin
...
ensure
sp.wait
end
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sized_parallel.rb', line 59 def initialize n = Etc.nprocessors if defined? yield then Pool.new n do |p| @p = p yield self end else @p = Pool.new n end end |
Instance Method Details
#start(*argv) {|*argv| ... } ⇒ Task
what on earth is the arguments that seems completely useless? Well the problem we are routing is inter-thread variable scope. For instance it is a WRONG idea to write code like this:
1024.times { |i| Thread.start { puts i } } # WRONG WRONG WROG WRONG
The code above behaves unexpectedly because the variable i is shared
across threads, overwritten on occasions. A valid usage is below:
1024.times { |i| Thread.start(i) {|j| puts j } }
# ^^^ ^^^ ^
Note the use of newly introduced block-parameter j. The same
discussion goes exactly the same way to this method, because obviously
this is a method that spawns (or reuses) a thread.
Starts a series of execution.
76 77 78 79 80 81 |
# File 'lib/sized_parallel.rb', line 76 def start *argv raise ArgumentError, 'no block given' unless defined? yield Task.new @p do yield(*argv) end end |
#wait ⇒ SizedParallel
Waits for tasks. It blocks until all the jobs are done.
85 86 87 88 |
# File 'lib/sized_parallel.rb', line 85 def wait @p.wait return self end |