Class: Parallizer

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

Defined Under Namespace

Classes: MethodCallNotifier, Proxy, Worker

Constant Summary collapse

DEFAULT_WORK_QUEUE_SIZE =
10
VERSION =
"0.4.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parallizer.



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

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.



30
31
32
# File 'lib/parallizer.rb', line 30

def call_infos
  @call_infos
end

#callsObject (readonly)

Returns the value of attribute calls.



30
31
32
# File 'lib/parallizer.rb', line 30

def calls
  @calls
end

#clientObject (readonly)

Returns the value of attribute client.



30
31
32
# File 'lib/parallizer.rb', line 30

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/parallizer.rb', line 30

def options
  @options
end

#proxyObject (readonly)

Returns the value of attribute proxy.



30
31
32
# File 'lib/parallizer.rb', line 30

def proxy
  @proxy
end

Class Method Details

.work_queueObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/parallizer.rb', line 18

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



10
11
12
# File 'lib/parallizer.rb', line 10

def work_queue_size
  @work_queue_size || DEFAULT_WORK_QUEUE_SIZE
end

.work_queue_size=(work_queue_size) ⇒ Object



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

def work_queue_size=(work_queue_size)
  @work_queue_size = work_queue_size
end

Instance Method Details

#addObject



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

def add
  ::Parallizer::MethodCallNotifier.new do |*args|
    add_call(*args)
  end
end

#add_call(method_name, *args) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/parallizer.rb', line 48

def add_call(method_name, *args)
  raise ArgumentError, "Cannot add calls after proxy has been generated" if @proxy

  method_name_and_args = [method_name.to_sym, *args]
  return if call_infos[method_name_and_args]

  call_info = {
    :future => ::Parallizer::work_queue.future(:run, @client, method_name, args, options),
    :result => nil,
    :exception => nil
  }
  call_infos[method_name_and_args] = call_info
end

#create_proxyObject

Raises:

  • (ArgumentError)


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

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

  execute

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