legion 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/legion/object.rb +2 -2
- data/lib/legion/supervisor.rb +20 -11
- data/lib/legion/version.rb +1 -1
- data/test/example.rb +6 -15
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f42434869f3fbd249e20f433fe13964d00157a4b
|
4
|
+
data.tar.gz: 462d1c607be4627681952c9968d2b9f6bf06f615
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6855d708d2fd03e7fc7ae3a301154e7efdd6fbafbb5b1796403c44e84ef3b06798c7b9c6727bdf7233b09b2f8c58018f750157ac08a47ce5bc10968d19cbff54
|
7
|
+
data.tar.gz: 21c09d6e2c68a8f0322fe3ffdbf7e7b5f597221f99b0c355f38acfba405e4cf5df45ce9f32f5d72d01cb978449ec9451c1d56d548607c41b8d1c3dc4b4450fd5
|
data/lib/legion/object.rb
CHANGED
@@ -6,8 +6,6 @@ module Legion
|
|
6
6
|
class Object
|
7
7
|
include MonitorMixin
|
8
8
|
|
9
|
-
attr_reader :pid, :uri
|
10
|
-
|
11
9
|
class << self
|
12
10
|
def method_added(name)
|
13
11
|
return if name =~ /_async\z/i
|
@@ -31,6 +29,8 @@ module Legion
|
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
32
|
+
attr_reader :pid, :uri
|
33
|
+
|
34
34
|
def ok?
|
35
35
|
true
|
36
36
|
end
|
data/lib/legion/supervisor.rb
CHANGED
@@ -5,23 +5,25 @@ module Legion
|
|
5
5
|
attr_reader(
|
6
6
|
:klass,
|
7
7
|
:processes,
|
8
|
+
:port,
|
8
9
|
:local_instances,
|
9
10
|
:remote_instances
|
10
11
|
)
|
11
12
|
|
12
|
-
def initialize(klass, processes: 1)
|
13
|
+
def initialize(klass, processes: 1, port: 42042)
|
13
14
|
@klass = klass
|
14
15
|
@processes = processes
|
16
|
+
@port = port
|
15
17
|
@local_instances = []
|
16
18
|
@remote_instances = []
|
17
19
|
end
|
18
20
|
|
19
|
-
def
|
21
|
+
def start
|
20
22
|
DRb.start_service
|
21
23
|
(1..processes).each do |i|
|
22
24
|
local_instance = klass.new
|
23
25
|
local_instance.start_remote_instance(port: port)
|
24
|
-
port += 1
|
26
|
+
@port += 1
|
25
27
|
local_instances << local_instance
|
26
28
|
remote_instance = DRbObject.new_with_uri(local_instance.uri)
|
27
29
|
verify_remote_instance(remote_instance)
|
@@ -29,22 +31,29 @@ module Legion
|
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
def
|
33
|
-
remote_instances.each
|
34
|
-
remote_instance.exit rescue nil
|
35
|
-
end
|
34
|
+
def stop
|
35
|
+
remote_instances.each { |inst| inst.exit }
|
36
36
|
DRb.stop_service
|
37
37
|
end
|
38
38
|
|
39
39
|
def get_remote_instance
|
40
|
-
@
|
41
|
-
remote_instance = remote_instances[@
|
40
|
+
@index ||= 0
|
41
|
+
remote_instance = remote_instances[@index]
|
42
42
|
sleep 0.01 while remote_instance.busy?
|
43
|
-
@
|
44
|
-
@
|
43
|
+
@index += 1
|
44
|
+
@index = 0 if @index >= remote_instances.length
|
45
45
|
remote_instance
|
46
46
|
end
|
47
47
|
|
48
|
+
def method_missing(name, *args)
|
49
|
+
get_remote_instance.send("#{name}_async", *args)
|
50
|
+
end
|
51
|
+
|
52
|
+
def respond_to?(name)
|
53
|
+
return true if super
|
54
|
+
klass.instance_methods.include? name.to_sym
|
55
|
+
end
|
56
|
+
|
48
57
|
protected
|
49
58
|
|
50
59
|
def verify_remote_instance(remote_instance)
|
data/lib/legion/version.rb
CHANGED
data/test/example.rb
CHANGED
@@ -1,28 +1,19 @@
|
|
1
1
|
require_relative "../lib/legion"
|
2
2
|
|
3
3
|
class Example < Legion::Object
|
4
|
+
before(:work) { puts "before work" }
|
4
5
|
|
5
6
|
def work(index)
|
7
|
+
puts "working on: #{index}"
|
6
8
|
sleep 2
|
7
9
|
end
|
8
|
-
|
9
|
-
before :work do
|
10
|
-
puts "before work"
|
11
|
-
end
|
12
|
-
|
13
10
|
end
|
14
11
|
|
15
12
|
def run_example
|
16
|
-
supervisor = Legion::Supervisor.new(Example, processes: 7)
|
17
|
-
supervisor.
|
18
|
-
|
19
|
-
|
20
|
-
worker = supervisor.get_remote_instance
|
21
|
-
worker.work_async(i)
|
22
|
-
puts i
|
23
|
-
end
|
24
|
-
|
25
|
-
supervisor.stop_remote_instances
|
13
|
+
supervisor = Legion::Supervisor.new(Example, processes: 7, port: 52042)
|
14
|
+
supervisor.start
|
15
|
+
1000.times { |i| supervisor.work(i) }
|
16
|
+
supervisor.stop
|
26
17
|
end
|
27
18
|
|
28
19
|
run_example
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: legion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
41
|
+
description: Concurrent processing made easy... even for MRI.
|
42
42
|
email:
|
43
43
|
- natehop@gmail.com
|
44
44
|
executables: []
|
@@ -73,7 +73,7 @@ rubyforge_project:
|
|
73
73
|
rubygems_version: 2.0.3
|
74
74
|
signing_key:
|
75
75
|
specification_version: 4
|
76
|
-
summary:
|
76
|
+
summary: Concurrent processing made easy... even for MRI.
|
77
77
|
test_files:
|
78
78
|
- test/example.rb
|
79
79
|
has_rdoc:
|