Class: Parallizer

Inherits:
Object
  • Object
show all
Includes:
HangingMethods
Defined in:
lib/parallizer.rb,
lib/parallizer/proxy.rb,
lib/parallizer/worker.rb,
lib/parallizer/version.rb

Defined Under Namespace

Classes: Proxy, Worker

Constant Summary collapse

DEFAULT_WORK_QUEUE_SIZE =
10
VERSION =
"0.4.7"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Parallizer

Returns a new instance of Parallizer.



41
42
43
44
45
# File 'lib/parallizer.rb', line 41

def initialize(client, options = {})
  @client = client
  @options = {:retries => 0}.merge(options)
  @call_infos = {}
end

Instance Attribute Details

#call_infosObject (readonly)

Returns the value of attribute call_infos.



39
40
41
# File 'lib/parallizer.rb', line 39

def call_infos
  @call_infos
end

#callsObject (readonly)

Returns the value of attribute calls.



39
40
41
# File 'lib/parallizer.rb', line 39

def calls
  @calls
end

#clientObject (readonly)

Returns the value of attribute client.



39
40
41
# File 'lib/parallizer.rb', line 39

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



39
40
41
# File 'lib/parallizer.rb', line 39

def options
  @options
end

#proxyObject (readonly)

Returns the value of attribute proxy.



39
40
41
# File 'lib/parallizer.rb', line 39

def proxy
  @proxy
end

Class Method Details

.in_parallizer_thread?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/parallizer.rb', line 34

def in_parallizer_thread?
  Thread.current[:parallizer_thread] == true
end

.work_queueObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/parallizer.rb', line 23

def work_queue
  # TODO: share the work queue among calling threads??
  queue = Thread.current[:parallizer_work_queue]
  if queue.nil? || Thread.current[:parallizer_work_queue_size] != work_queue_size
    queue = Thread.current[:parallizer_work_queue] = ::Parallizer::Worker.pool(:size => work_queue_size)
    Thread.current[:parallizer_work_queue_size] = work_queue_size
  end
  
  queue
end

.work_queue_sizeObject



15
16
17
# File 'lib/parallizer.rb', line 15

def work_queue_size
  @work_queue_size || DEFAULT_WORK_QUEUE_SIZE
end

.work_queue_size=(work_queue_size) ⇒ Object



19
20
21
# File 'lib/parallizer.rb', line 19

def work_queue_size=(work_queue_size)
  @work_queue_size = work_queue_size
end

Instance Method Details

#add_call(method_name, *args) ⇒ Object



51
52
53
# File 'lib/parallizer.rb', line 51

def add_call(method_name, *args)
  add.send(method_name, *args)
end

#all_call_resultsObject



63
64
65
66
67
68
69
70
71
# File 'lib/parallizer.rb', line 63

def all_call_results
  proxy = create_proxy

  call_infos.keys.inject({}) do |result, method_name_and_args|
    result[method_name_and_args] = proxy.send(*method_name_and_args)

    result
  end
end

#create_proxyObject

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
# File 'lib/parallizer.rb', line 55

def create_proxy
  raise ArgumentError, "Cannot create another proxy" if @proxy

  execute

  ::Parallizer::Proxy.new(client, call_infos)
end