Class: Pthread::Pthread
- Inherits:
-
Object
- Object
- Pthread::Pthread
- 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
-
.add_executor(queue = nil) ⇒ Object
Adds a single executor on the same machine as the main programm.
-
.add_executors(count = 1, queue = nil) ⇒ Object
Adds executors on the same machine as the main programm.
-
.kill_executors ⇒ Object
Kills all launched executors on this machine.
-
.start_service(host) ⇒ Object
Starts the drb server.
Instance Method Summary collapse
-
#initialize(job) ⇒ Pthread
constructor
Initializes new pthread and schedules execution of the job.
-
#value ⇒ Object
Returns value of a pthread.
Constructor Details
#initialize(job) ⇒ Pthread
Initializes new pthread and schedules execution of the job.
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.
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.
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_executors ⇒ Object
Kills all launched executors on this machine.
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.
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
#value ⇒ Object
If value is not yet calculated it will block the execution.
If pthread resulted in an exception it will be raised.
Returns 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 |