Class: Asynchronous::Concurrency

Inherits:
CleanClass show all
Defined in:
lib/asynchronous/concurrency.rb

Overview

you can use simple :c also instead of :concurrency remember :concurrency is all about GIL case, so you can modify the objects in memory This is ideal for little operations in simultaneously or when you need to update objects in the memory

Instance Method Summary collapse

Constructor Details

#initialize(callable) ⇒ Concurrency

Returns a new instance of Concurrency.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/asynchronous/concurrency.rb', line 10

def initialize(callable)
  begin
    @value= nil
    @try_count= 0
    @rescue_state= nil
    @thread ||= ::Thread.new { callable.call }
    @rescue_state= nil
  rescue ThreadError
    @rescue_state ||= true
    @try_count += 1
    if 3 <= @try_count
      @value= callable.call
      @rescue_state= nil
    else
      sleep 5
      retry
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



55
56
57
# File 'lib/asynchronous/concurrency.rb', line 55

def method_missing(method, *args)
  value.__send__(method, *args)
end

Instance Method Details

#inspectObject



47
48
49
50
51
52
53
# File 'lib/asynchronous/concurrency.rb', line 47

def inspect
  if @thread.alive?
    "#<Async running>"
  else
    value.inspect
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/asynchronous/concurrency.rb', line 59

def respond_to_missing?(method, include_private = false)
  value.respond_to?(method, include_private)
end

#valueObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/asynchronous/concurrency.rb', line 30

def value

  if @value.nil?
    until @rescue_state.nil?
      sleep 1
    end
    @value= @thread.value
  end

  return @value

end

#value=(obj) ⇒ Object



43
44
45
# File 'lib/asynchronous/concurrency.rb', line 43

def value=(obj)
  @value= obj
end