Class: Pthread::Pthread

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

Overview

The Pthread class is the main class that users work with. It used for creating forks to be executed in a separate processes including on remote machines.

Constant Summary collapse

@@ts =
Rinda::TupleSpace.new
@@pids =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ Pthread

Initializes new pthread and schedules execution of the job.

Examples:

Initialize new parrallel job

pthread = Pthread::Pthread.new queue: 'tasks', code: %{
  x ** 2
}, context: { x: 5 }

Parameters:

  • job (Hash)

    should contain :code, :context and optionally :queue



71
72
73
# File 'lib/pthread/pthread.rb', line 71

def initialize(job)
  @@ts.write([self.object_id, job[:queue], job[:code], job[:context]])
end

Class Method Details

.add_executor(queue = nil) ⇒ Object

Adds a single executor on the same machine as the main programm.

Examples:

Add an executor without a queue

Pthread::Pthread.add_executor

Add an executor for a specific queue

Pthread::Pthread.add_executor, :tasks

Parameters:

  • queue (Symbol, String) (defaults to: nil)

    name of the queue for executor to be attached to.



50
51
52
# File 'lib/pthread/pthread.rb', line 50

def self.add_executor(queue=nil)
  add_executors(1, queue)
end

.add_executors(count = 1, queue = nil) ⇒ Object

Adds executors on the same machine as the main programm.

Examples:

Add executors without a queue

Pthread::Pthread.add_executors 5

Add executors for specific queue

Pthread::Pthread.add_executors 5, :tasks

Parameters:

  • count (FixNum) (defaults to: 1)

    amount of executors to start.

  • queue (Symbol, String) (defaults to: nil)

    name of the queue for executors to be attached to.



33
34
35
36
37
38
39
40
# File 'lib/pthread/pthread.rb', line 33

def self.add_executors(count = 1, queue=nil)
  count.times do
    @@pids << fork do
      DRb.stop_service
      PthreadExecutor.new(@@host, queue)
    end
  end
end

.kill_executorsObject

Kills all launched executors on this machine.

Examples:

Add executors without a queue

Pthread::Pthread.kill_executors


58
59
60
61
# File 'lib/pthread/pthread.rb', line 58

def self.kill_executors
  Process.kill 'HUP', *@@pids
  @@pids = []
end

.start_service(host) ⇒ Object

Starts the drb server.

Examples:

Start service

Pthread::Pthread.start_service '192.168.1.100:12345'

Parameters:

  • host (String)

    that contains host url and port.



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

def self.start_service(host)
  @@host = host
  DRb.start_service("druby://#{@@host}", @@ts)
end

Instance Method Details

#valueObject

Note:

If value is not yet calculated it will block the execution.

Note:

If pthread resulted in an exception it will be raised.

Returns value of a pthread.

Examples:

pthread.value

Returns:

  • (Object)

    value of a pthread.



85
86
87
# File 'lib/pthread/pthread.rb', line 85

def value
  raw_value.is_a?(StandardError) ? raise(raw_value) : raw_value
end