pthread 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b4115d829039a0033707e7ee8c61c20b9811ce8
4
- data.tar.gz: 914e7300414d2f88e9e0c4a1c570f49b1168e46e
3
+ metadata.gz: dafcdd2900f027b28215e12e83f2aa4aeff4a59c
4
+ data.tar.gz: 31fa0fab9742a9c6fe677e62a6be238f89761591
5
5
  SHA512:
6
- metadata.gz: f55558ad0dce52d87478486f6c3e072bceecde87eaac811440f700b3b324f2cfc49f8c75b419290df2972c794361cb41042be2eb331a9fc2ba2eb42f3ee3c758
7
- data.tar.gz: ad5ca10800236855b77ca24696d1a4828f62568f5e50c0259b270848fea202b16a0915d38989010f9a46f5ee5f8759439786babaedd86e79e5838aadaef7bd1b
6
+ metadata.gz: 9453e1c25209cb0d5ecabf94b5c5323b7bd7f7527d7a7fd20f3ff28b18c81ea095c561e6159ffd02edc527cfd1cb8f67457295f4dcf402b57b2f59cd33a9df2d
7
+ data.tar.gz: 882c89a3653ce66bb42acc97d94c7735d9a50df5607d198a565036d35cd9eb19ae74b9621ad3037b5ad7fa9c75bba09197011c940b8d670650a5ee0c5c554e96
@@ -1,43 +1,100 @@
1
1
  require 'rinda/tuplespace'
2
2
 
3
- class Pthread::Pthread
4
- @@ts = Rinda::TupleSpace.new
5
- @@pids = []
3
+ module Pthread
6
4
 
7
- def self.start_service(host)
8
- @@host = host
9
- DRb.start_service("druby://#{@@host}", @@ts)
10
- end
5
+ # The +Pthread+ class is the main class that users work with.
6
+ # It used for creating forks to be executed in a separate processes
7
+ # including other machines
8
+ class Pthread
9
+
10
+ @@ts = Rinda::TupleSpace.new
11
+ @@pids = []
12
+
13
+ # Starts the drb server
14
+ #
15
+ # @param [ String ] that contains host url and port
16
+ #
17
+ # @example Start service
18
+ # Pthread::Pthread.start_service '192.168.1.100:12345'
19
+ def self.start_service(host)
20
+ @@host = host
21
+ DRb.start_service("druby://#{@@host}", @@ts)
22
+ end
11
23
 
12
- def self.add_executors(count = 1, queue=nil)
13
- count.times do
14
- @@pids << fork do
15
- DRb.stop_service
16
- Pthread::PthreadExecutor.new(@@host, queue)
24
+ # Adds executors on the same machine as the main programm
25
+ #
26
+ # @param [ FixNum ] amount of executors to start
27
+ # @param [ Symbol, String ] name of the queue for executors to be attached to
28
+ #
29
+ # @example Add executors without a queue
30
+ # Pthread::Pthread.add_executors 5
31
+ # @example Add executors for specific queue
32
+ # Pthread::Pthread.add_executors 5, :tasks
33
+
34
+ def self.add_executors(count = 1, queue=nil)
35
+ count.times do
36
+ @@pids << fork do
37
+ DRb.stop_service
38
+ Pthread::PthreadExecutor.new(@@host, queue)
39
+ end
17
40
  end
18
41
  end
19
- end
20
42
 
21
- def self.add_executor(queue=nil)
22
- add_executors(1, queue)
23
- end
43
+ # Adds a single executor on the same machine as the main programm
44
+ #
45
+ # @param [ Symbol, String ] name of the queue for executor to be attached to
46
+ #
47
+ # @example Add executor without a queue
48
+ # Pthread::Pthread.add_executor
49
+ # @example Add executor for specific queue
50
+ # Pthread::Pthread.add_executor, :tasks
51
+ def self.add_executor(queue=nil)
52
+ add_executors(1, queue)
53
+ end
24
54
 
25
- def self.kill_executors
26
- Process.kill 'HUP', *@@pids
27
- @@pids = []
28
- end
55
+ # Kills all launched executors on this machine
56
+ #
57
+ # @example Add executors without a queue
58
+ # Pthread::Pthread.kill_executors
59
+ def self.kill_executors
60
+ Process.kill 'HUP', *@@pids
61
+ @@pids = []
62
+ end
29
63
 
30
- def initialize(job)
31
- @@ts.write([self.object_id, job[:queue], job[:code], job[:context]])
32
- end
64
+ # Initializes new pthread and schedules execution of the job
65
+ #
66
+ # @param [ Hash ] should containt :code, :context and optionally :queue
67
+ #
68
+ # @example Initialize new parrallel job
69
+ # pthread = Pthread::Pthread.new queue: 'tasks', code: %{
70
+ # x ** 2
71
+ # }, context: { x: 5 }
72
+ def initialize(job)
73
+ @@ts.write([self.object_id, job[:queue], job[:code], job[:context]])
74
+ end
33
75
 
34
- def value
35
- raw_value.is_a?(StandardError) ? raise(raw_value) : raw_value
36
- end
37
76
 
38
- private
77
+ # Returns value of a pthread
78
+ #
79
+ # @note If value if not yet calculated will block the execution
80
+ # @note If pthread resulted in an exception it will be raised
81
+ #
82
+ # @example pthread.value
83
+ #
84
+ # @return [ Object ] value of a pthread
85
+ def value
86
+ raw_value.is_a?(StandardError) ? raise(raw_value) : raw_value
87
+ end
88
+
89
+ private
39
90
 
40
- def raw_value
41
- @raw_value ||= @@ts.take([self.object_id, nil])[1]
91
+ # Returns raw value of a pthread even if it was an exception
92
+ #
93
+ # @note If value if not yet calculated will block the execution
94
+ #
95
+ # @return [ Object ] raw value of a pthread
96
+ def raw_value
97
+ @raw_value ||= @@ts.take([self.object_id, nil])[1]
98
+ end
42
99
  end
43
100
  end
@@ -1,27 +1,40 @@
1
1
  require 'drb/drb'
2
2
 
3
- class Pthread::PthreadExecutor
4
- def initialize(host, queue=nil)
5
- DRb.start_service
6
- ts = DRbObject.new_with_uri("druby://#{host}")
3
+ module Pthread
7
4
 
8
- loop do
9
- pthread_id, _, code, context = ts.take([nil, queue, nil, nil])
5
+ # +PthreadExecutor+ is used by Pthread to run code in a separate fork.
6
+ # Users can you this class to start executors manually on remote machines.
7
+ class PthreadExecutor
10
8
 
11
- context && context.each do |a, v|
12
- singleton_class.class_eval { attr_accessor a }
13
- self.send("#{a}=", context[a])
14
- end
9
+ # Initliazes new executor.
10
+ #
11
+ # @param [ String ] DRB host that has main programm running
12
+ # @param [ String, Symbol ] optinal queue name to attach executor
13
+ #
14
+ # @example Connect to remote Drb service
15
+ # Pthread::PthreadExecutor.new '192.168.1.100:12345', :tasks
16
+ def initialize(host, queue=nil)
17
+ DRb.start_service
18
+ ts = DRbObject.new_with_uri("druby://#{host}")
15
19
 
16
- value = begin
17
- eval(code)
18
- rescue => e
19
- e
20
- end
20
+ loop do
21
+ pthread_id, _, code, context = ts.take([nil, queue, nil, nil])
22
+
23
+ context && context.each do |a, v|
24
+ singleton_class.class_eval { attr_accessor a }
25
+ self.send("#{a}=", context[a])
26
+ end
21
27
 
22
- ts.write([pthread_id, value])
28
+ value = begin
29
+ eval(code)
30
+ rescue => e
31
+ e
32
+ end
33
+
34
+ ts.write([pthread_id, value])
35
+ end
36
+ rescue DRb::DRbConnError
37
+ exit 0
23
38
  end
24
- rescue DRb::DRbConnError
25
- exit 0
26
39
  end
27
40
  end
@@ -1,3 +1,3 @@
1
1
  module Pthread
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pthread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Cernovs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-18 00:00:00.000000000 Z
11
+ date: 2013-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler