thimble 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b25fd7c92e8dafb66ba3bd700e82e25cb3cbf4f
4
- data.tar.gz: 2f6d476abbfdb1d1cdad7363c865592995252d1f
3
+ metadata.gz: e14df83bf17841cdd0087c6c2bdf39f1f9b6644e
4
+ data.tar.gz: d6d77a89dcc0c3b333e44ba758f3b9b6c1709369
5
5
  SHA512:
6
- metadata.gz: aa40124bea0b95345aa758fa667d264343b34296829854b954f7f6652cd177caaacac713c507a5e467eb846adc733a273fbb52d3f58e5aad99b3b1ee3cfa1240
7
- data.tar.gz: 85144ec759b08b56f60b18fb0a790d9c927b9efbcb92665ca5d2c4f9e922bcc3bd88ca1e7a17ef1f163577b3cca11c75fc3219934934c997b976553759c040b6
6
+ metadata.gz: 1c9b4bfbf03768af041193bd4c91fdafe4b4e73e39aabafdd0668c1a88f2679b0689dd3b6f888f9df2606de6d348c2f15a9bc8a2b186cd6ebdab1edde920cb1e
7
+ data.tar.gz: e5866041ac437c858fec5a0dd4b25f3debe955f5852528dab93b961e259d30796fd9607662a0811b7c58490e310c58d5472bd2550ebfa4aece4da5d417b1f120
data/lib/QueueItem.rb CHANGED
@@ -9,7 +9,7 @@ module Thimble
9
9
  end
10
10
 
11
11
  def to_s
12
- "#{@name}: #{@item} \nID: #{@id}"
12
+ "#{@name}: #{@item} ID: #{@id}"
13
13
  end
14
14
 
15
15
  end
data/lib/Thimble.rb CHANGED
@@ -8,32 +8,56 @@ require 'ostruct'
8
8
  module Thimble
9
9
 
10
10
  class Thimble < ThimbleQueue
11
- def initialize(array, manager = Manager.new)
11
+ def initialize(array, manager = Manager.new, result = nil, name = "Main")
12
12
  raise ArgumentError.new ("You need to pass a manager to Thimble!") unless manager.class == Manager
13
13
  raise ArgumentError.new ("There needs to be an iterable object passed to Thimble to start.") unless array.respond_to? :each
14
+ @result = if result.nil?
15
+ ThimbleQueue.new(array.size, "Result")
16
+ else
17
+ result
18
+ end
19
+ raise ArgumentError.new ("result needs to be an open ThimbleQueue") unless (@result.class == ThimbleQueue && !@result.closed?)
14
20
  @manager = manager
15
- @result = ThimbleQueue.new(array.size, "Result")
16
21
  @running = true
17
- super(array.size, "Main")
22
+ super(array.size, name)
23
+ @logger.debug("loading thimble #{name}")
18
24
  array.each {|item| push(item)}
25
+ @logger.debug("finished loading thimble #{name}")
19
26
  close()
20
27
  end
21
28
 
22
- # This will use the manager and tranform your thimble queue.
29
+ # This will use the manager and transform your thimble queue.
23
30
  # requires a block
24
- # returns a new ThimbleQueue
31
+ # @return [ThimbleQueue]
25
32
  def map
33
+ @logger.debug("starting map in #{@name} with id #{Thread.current.object_id}")
26
34
  @running = true
27
35
  while @running
28
36
  manage_workers &Proc.new
29
37
  end
30
38
  @result.close()
39
+ @logger.debug("finishing map in #{@name} with id #{Thread.current.object_id}")
40
+ @result
41
+ end
42
+
43
+ # This will use the manager and transform the thimble queue asynchronously.
44
+ # Will return the result instantly, so you can use it for next stage processing.
45
+ # requires a block
46
+ # @return [ThimbleQueue]
47
+ def map_async
48
+ @logger.debug("starting async map in #{@name} with id #{Thread.current.object_id}")
49
+ @logger.debug("queue: #{@queue}")
50
+ Thimble.async do
51
+ map &Proc.new
52
+ end
53
+ @logger.debug("finished async map in #{@name} with id #{Thread.current.object_id}")
31
54
  @result
32
55
  end
33
56
 
34
57
  # Will perform anything handed to this asynchronously.
35
58
  # Requires a block
36
- def self.a_sync
59
+ # @return [Thread]
60
+ def self.async
37
61
  Thread.new do |e|
38
62
  yield e
39
63
  end
@@ -55,12 +79,12 @@ module Thimble
55
79
  end
56
80
 
57
81
  def manage_workers
58
- while (@manager.worker_available? && batch = get_batch)
59
- @manager.sub_worker( @manager.get_worker(batch, &Proc.new), @id)
60
- end
61
82
  @manager.current_workers(@id).each do |pid, pair|
62
83
  get_result(pair.worker)
63
84
  end
85
+ while (@manager.worker_available? && batch = get_batch)
86
+ @manager.sub_worker( @manager.get_worker(batch, &Proc.new), @id)
87
+ end
64
88
  @running = false if !@manager.working? && !batch
65
89
  end
66
90
 
data/lib/ThimbleQueue.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'thread'
2
+ require 'logger'
2
3
  require_relative 'QueueItem'
3
4
 
4
5
  module Thimble
@@ -16,6 +17,12 @@ module Thimble
16
17
  @close_now = false
17
18
  @empty = ConditionVariable.new
18
19
  @full = ConditionVariable.new
20
+ @logger = Logger.new(STDOUT)
21
+ @logger.sev_threshold = Logger::UNKNOWN
22
+ end
23
+
24
+ def setLogger(level)
25
+ @logger.sev_threshold = level
19
26
  end
20
27
 
21
28
  def each
@@ -24,10 +31,15 @@ module Thimble
24
31
  end
25
32
  end
26
33
 
34
+ # Returns the size of the ThimbleQueue
35
+ # @return [Fixnum]
27
36
  def length
28
37
  size
29
38
  end
30
39
 
40
+ # Will concatenate an enumerable to the ThimbleQueue
41
+ # @param [Enumerable]
42
+ # @return [ThimbleQueue]
31
43
  def +(other)
32
44
  raise ArgumentError.new("+ requires another Enumerable!") unless other.class < Enumerable
33
45
  merged_thimble = ThimbleQueue.new(length + other.length, @name)
@@ -36,15 +48,20 @@ module Thimble
36
48
  merged_thimble
37
49
  end
38
50
 
51
+ # Returns the first item in the queue
52
+ # @return [Object]
39
53
  def next
40
54
  @mutex.synchronize do
41
55
  while !@close_now
42
56
  a = @queue.shift
57
+ @logger.debug("#{@name}'s queue shifted to: #{a}")
43
58
  if !a.nil?
44
59
  @full.broadcast
60
+ @empty.broadcast
45
61
  return a
46
62
  else
47
- return nil if @closed
63
+ @logger.debug("#{@name}'s queue is currently closed?: #{closed?}")
64
+ return nil if closed?
48
65
  @empty.wait(@mutex)
49
66
  end
50
67
  end
@@ -52,41 +69,58 @@ module Thimble
52
69
  end
53
70
 
54
71
  # This will push whatever it is handed to the queue
72
+ # @param [Object]
55
73
  def push(x)
56
74
  raise RuntimeError.new("Queue is closed!") if @closed
75
+ @logger.debug("Pushing into #{@name} values: #{x}")
57
76
  @mutex.synchronize do
58
77
  while !offer(x)
59
78
  @full.wait(@mutex)
79
+ @logger.debug("#{@name} is waiting on full")
60
80
  end
61
81
  @empty.broadcast
62
82
  end
83
+ @logger.debug("Finished pushing int #{@name}: #{x}")
63
84
  end
64
85
 
65
86
  # This will flatten any nested arrays out and feed them one at
66
87
  # a time to the queue.
88
+ # @param [Object, Enumerable]
89
+ # @return [nil]
67
90
  def push_flat(x)
68
91
  raise RuntimeError.new("Queue is closed!") if @closed
92
+ @logger.debug("Pushing flat into #{@name} values: #{x}")
69
93
  if x.respond_to? :each
70
94
  x.each {|item| push(item)}
71
95
  else
72
96
  @mutex.synchronize do
73
97
  while !offer(x)
98
+ @logger.debug("#{@name} is waiting on full")
74
99
  @full.wait(@mutex)
75
100
  end
76
101
  @empty.broadcast
77
102
  end
78
103
  end
104
+ @logger.debug("Finished pushing flat into #{@name} values: #{x}")
79
105
  end
80
106
 
107
+ # Closes the ThibleQueue
108
+ # @param [TrueClass, FalseClass]
109
+ # @return [nil]
81
110
  def close(now = false)
111
+ raise ArgumentError.new("now must be true or false") unless (now == true || now == false)
112
+ @logger.debug("#{@name} is closing")
82
113
  @mutex.synchronize do
83
114
  @closed = true
84
115
  @close_now = true if now
85
116
  @full.broadcast
86
117
  @empty.broadcast
87
118
  end
119
+ @logger.debug("#{@name} is closed: #{@closed} now: #{@close_now}")
88
120
  end
89
121
 
122
+ # Will force the ThimbleQueue into an array
123
+ # @return [Array[Object]]
90
124
  def to_a
91
125
  a = []
92
126
  while item = self.next
@@ -95,14 +129,17 @@ module Thimble
95
129
  a
96
130
  end
97
131
 
132
+ # checks if the ThimbleQueue is closed
133
+ # @return [TrueClass, FalseClass]
98
134
  def closed?
99
- @close
135
+ @closed
100
136
  end
101
137
 
102
138
  private
103
139
  def offer(x)
104
140
  if @queue.size < @size
105
141
  @queue << QueueItem.new(x)
142
+ @empty.broadcast
106
143
  true
107
144
  else
108
145
  false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thimble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kovanda