parallelize 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@
5
5
  * Added `pmap`
6
6
 
7
7
  === 0.2.1 / 2011/08/02
8
- * Compatible to Ruby 1.8
8
+ * Compatible to Ruby 1.8
9
9
 
10
10
  === 0.2.0 / 2011/08/02
11
11
  * Initial release
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -6,6 +6,6 @@ require 'parallelize/enumerable_ext'
6
6
  # @param [Fixnum] num_threads Number of concurrent threads
7
7
  # @param [Boolean] collect_exceptions If true, waits for all threads to complete even in case of exception, and throws ParallelException at the end. If false exception is immediately thrown.
8
8
  def parallelize num_threads, collect_exceptions = false, &block
9
- num_threads.times.peach(num_threads, collect_exceptions, &block)
9
+ num_threads.times.peach(num_threads, collect_exceptions, &block)
10
10
  end
11
11
 
@@ -19,8 +19,11 @@ module Enumerable
19
19
 
20
20
  begin
21
21
  prev_trap = trap('INT') { Thread.current.raise Interrupt }
22
- enum.each_slice((enum.count{true} / num_threads.to_f).ceil) do |slice|
23
- threads <<
22
+ count = enum.count{true}
23
+ return [] if count == 0
24
+
25
+ enum.each_slice((count / num_threads.to_f).ceil) do |slice|
26
+ threads <<
24
27
  case block.arity
25
28
  when 2
26
29
  Thread.new(slice, threads.length) { |my_slice, thread_idx|
@@ -75,18 +78,18 @@ module Enumerable
75
78
  end
76
79
  end
77
80
 
78
- # Divides the Enumerable objects into pieces and execute with multiple threads
79
- # @return [Array] Threads.
80
- # @param [Fixnum] num_threads Number of concurrent threads
81
- # @param [Boolean] collect_exceptions If true, waits for all threads to complete even in case of exception, and throws ParallelException at the end. If false exception is immediately thrown.
82
- def peach num_threads, collect_exceptions = false, &block
81
+ # Divides the Enumerable objects into pieces and execute with multiple threads
82
+ # @return [Array] Threads.
83
+ # @param [Fixnum] num_threads Number of concurrent threads
84
+ # @param [Boolean] collect_exceptions If true, waits for all threads to complete even in case of exception, and throws ParallelException at the end. If false exception is immediately thrown.
85
+ def peach num_threads, collect_exceptions = false, &block
83
86
  Parallelize.it self, num_threads, :each, collect_exceptions, &block
84
87
  end
85
88
 
86
89
  # Parallelized map.
87
- # @return [Array] Map function output for each element
88
- # @param [Fixnum] num_threads Number of concurrent threads
89
- # @param [Boolean] collect_exceptions If true, waits for all threads to complete even in case of exception, and throws ParallelException at the end. If false exception is immediately thrown.
90
+ # @return [Array] Map function output for each element
91
+ # @param [Fixnum] num_threads Number of concurrent threads
92
+ # @param [Boolean] collect_exceptions If true, waits for all threads to complete even in case of exception, and throws ParallelException at the end. If false exception is immediately thrown.
90
93
  def pmap num_threads, collect_exceptions = false, &block
91
94
  Parallelize.it self, num_threads, :map, collect_exceptions, &block
92
95
  end
@@ -1,14 +1,14 @@
1
1
  class ParallelException < Exception
2
- # @return [Hash] Hash of exceptions thrown. Indexed by thread index.
3
- attr_reader :exceptions
2
+ # @return [Hash] Hash of exceptions thrown. Indexed by thread index.
3
+ attr_reader :exceptions
4
4
 
5
- def initialize(exceptions)
6
- @exceptions = exceptions
7
- end
5
+ def initialize(exceptions)
6
+ @exceptions = exceptions
7
+ end
8
8
 
9
- def to_s
10
- "Exceptions thrown during parallel execution: [#{@exceptions.inspect}]"
11
- end
9
+ def to_s
10
+ "Exceptions thrown during parallel execution: [#{@exceptions.inspect}]"
11
+ end
12
12
  end
13
13
 
14
14
 
@@ -128,6 +128,10 @@ class TestParallelize < Test::Unit::TestCase
128
128
  end
129
129
  end
130
130
 
131
+ def test_empty_peach
132
+ [].peach(4) {}
133
+ end
134
+
131
135
  def test_pmap
132
136
  thr = 8
133
137
  n_per_thr = (101.0 / 8).ceil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parallelize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
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-06-19 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yard
@@ -81,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  segments:
83
83
  - 0
84
- hash: -3400073306691178831
84
+ hash: 3252524563698678207
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  none: false
87
87
  requirements:
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 1.8.24
93
+ rubygems_version: 1.8.25
94
94
  signing_key:
95
95
  specification_version: 3
96
96
  summary: Simple multi-threading for Ruby