Class: AnagramSolver::AsyncConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/anagram_solver/async_consumer.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue = Queue.new, &block) ⇒ AsyncConsumer

Returns a new instance of AsyncConsumer.



24
25
26
27
28
29
30
# File 'lib/anagram_solver/async_consumer.rb', line 24

def initialize(queue=Queue.new, &block)
  @queue  = queue
  @thread = Thread.new { consume }
  @mutex  = Mutex.new
  @block  = block
  @lock   = true
end

Class Attribute Details

.threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/anagram_solver/async_consumer.rb', line 8

def thread
  @thread
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



22
23
24
# File 'lib/anagram_solver/async_consumer.rb', line 22

def block
  @block
end

#mutexObject (readonly)

Returns the value of attribute mutex.



22
23
24
# File 'lib/anagram_solver/async_consumer.rb', line 22

def mutex
  @mutex
end

#queueObject (readonly)

Returns the value of attribute queue.



21
22
23
# File 'lib/anagram_solver/async_consumer.rb', line 21

def queue
  @queue
end

#threadObject (readonly)

Returns the value of attribute thread.



21
22
23
# File 'lib/anagram_solver/async_consumer.rb', line 21

def thread
  @thread
end

Class Method Details

.bg_process(to_be_processed, &block) ⇒ Object



10
11
12
13
14
# File 'lib/anagram_solver/async_consumer.rb', line 10

def bg_process(to_be_processed, &block)
  @thread = Thread.new(to_be_processed) { |p|
    block.call(p)
  }
end

.waitObject



16
17
18
# File 'lib/anagram_solver/async_consumer.rb', line 16

def wait
  thread.join
end

Instance Method Details

#consumeObject

Consumes data from a queue. NOTE: If I was to use a while loop here, I’d have to find a way of stoping the loop. A common way would be pushing nil to queue and then it would exit. However I’d have to be passing nil ATFT. If I didn’t pass nil it would raise:

Failure/Error:
(anonymous error class);
No live threads left. Deadlock?

As there’s nothing in queue.

In Ruby you have a lot of different ways to make the same thing.

This implentation simply asks queue what it’s size is and then loops through it (i.e times) and then I call queue.deq which stands for deqeuing.



53
54
55
56
57
58
59
60
# File 'lib/anagram_solver/async_consumer.rb', line 53

def consume
  (queue.size).times do
    mutex.synchronize {
      block.call(queue.deq)
      @lock = true
    }
  end
end

#finished?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/anagram_solver/async_consumer.rb', line 88

def finished?
  lock
end

#push(*args) ⇒ Object

Pushes args in to a queue.



65
66
67
68
# File 'lib/anagram_solver/async_consumer.rb', line 65

def push(*args)
  queue.push(*args)
  mutex.synchronize { @lock = false }
end

#wait_thread_to_finish!(ttw = 0) ⇒ Object

Waits for a thread to finish returns nil if limit seconds have past. or returns thread itself if thread was finished whithing limit seconds.

If you call it without a limit second, it will use 0 as default, therefore it will not wait thread to finish, and it will return nil. (i.e Stops main thread ( current one ) and waits until the other thread is finished, then passes execution to main thread again. )



84
85
86
# File 'lib/anagram_solver/async_consumer.rb', line 84

def wait_thread_to_finish!(ttw=0)
  thread.join(ttw)
end