picky 3.1.7 → 3.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/picky/cores.rb +23 -33
  2. metadata +3 -3
data/lib/picky/cores.rb CHANGED
@@ -16,59 +16,49 @@ module Picky
16
16
  # Options include:
17
17
  # * max: Maximum # of processors to use. Default is all it can get.
18
18
  #
19
- def self.forked ary_or_generator, options = {}
20
- return if ary_or_generator.empty?
19
+ def self.forked elements, options = {}, &block
20
+ return if elements.empty?
21
21
  raise "Block argument needed when running Cores.forked" unless block_given?
22
22
 
23
- ary_or_generator = ary_or_generator.sort_by { rand } if options[:randomly]
24
- generator = ary_or_generator.each
25
-
26
- # Don't fork if there's just one element.
23
+ # Note: Not using a generator because Enumerator#each
24
+ # is exhibiting problems in some Rubies on some OSs
25
+ # (see e.g. http://redmine.ruby-lang.org/issues/5003).
27
26
  #
28
- if generator.inject(0) { |total, element| total + 1 } == 1
29
- generator.each do |element|
30
- yield element # THINK yield generator.next results in trouble. Why?
31
- end
32
- return
33
- end
27
+ elements = elements.dup
28
+ elements = elements.sort_by { rand } if options[:randomly]
34
29
 
35
30
  # Get the maximum number of processors.
36
31
  #
37
- max = max_processors options
38
- currently_processing = 0
32
+ max = max_processors options
33
+ processing = 0
39
34
 
40
- #
41
- #
42
35
  loop do
43
- # Ramp it up to num processors.
44
- #
45
- while currently_processing < max
46
-
47
- currently_processing = currently_processing + 1
48
-
49
- element = nil
50
- begin
51
- element = generator.next
52
- rescue StopIteration => si
53
- break
54
- end
36
+ while processing < max
37
+ # Get the next element
38
+ #
39
+ element = elements.shift
55
40
  break unless element
41
+ processing += 1
56
42
 
43
+ # Fork and yield.
44
+ #
57
45
  Process.fork do
58
- sleep 0.01*currently_processing
59
- yield element
46
+ sleep 0.05*processing
47
+ block.call element
60
48
  end
61
-
62
49
  end
63
50
 
51
+ # Block and wait for any child to finish.
52
+ #
64
53
  begin
65
- Process.wait 0 # Block and wait for any child to finish.
54
+ Process.wait 0
66
55
  rescue Errno::ECHILD => e
67
56
  break
68
57
  ensure
69
- currently_processing = currently_processing - 1
58
+ processing -= 1
70
59
  end
71
60
  end
61
+
72
62
  end
73
63
 
74
64
  # Return the number of maximum usable processors.
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.1.7
5
+ version: 3.1.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florian Hanke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-16 00:00:00 +02:00
13
+ date: 2011-10-12 00:00:00 +02:00
14
14
  default_executable: picky
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - "="
34
34
  - !ruby/object:Gem::Version
35
- version: 3.1.7
35
+ version: 3.1.8
36
36
  type: :development
37
37
  version_requirements: *id002
38
38
  description: Fast Ruby semantic text search engine with comfortable single field interface.