pthread 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pthread/pthread.rb +86 -29
- data/lib/pthread/pthread_executor.rb +31 -18
- data/lib/pthread/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dafcdd2900f027b28215e12e83f2aa4aeff4a59c
|
4
|
+
data.tar.gz: 31fa0fab9742a9c6fe677e62a6be238f89761591
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9453e1c25209cb0d5ecabf94b5c5323b7bd7f7527d7a7fd20f3ff28b18c81ea095c561e6159ffd02edc527cfd1cb8f67457295f4dcf402b57b2f59cd33a9df2d
|
7
|
+
data.tar.gz: 882c89a3653ce66bb42acc97d94c7735d9a50df5607d198a565036d35cd9eb19ae74b9621ad3037b5ad7fa9c75bba09197011c940b8d670650a5ee0c5c554e96
|
data/lib/pthread/pthread.rb
CHANGED
@@ -1,43 +1,100 @@
|
|
1
1
|
require 'rinda/tuplespace'
|
2
2
|
|
3
|
-
|
4
|
-
@@ts = Rinda::TupleSpace.new
|
5
|
-
@@pids = []
|
3
|
+
module Pthread
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
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
|
-
|
41
|
-
|
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
|
-
|
4
|
-
def initialize(host, queue=nil)
|
5
|
-
DRb.start_service
|
6
|
-
ts = DRbObject.new_with_uri("druby://#{host}")
|
3
|
+
module Pthread
|
7
4
|
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
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
|
data/lib/pthread/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|