async-container 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/async/container.rb +2 -2
- data/lib/async/container/controller.rb +22 -0
- data/lib/async/container/forked.rb +4 -0
- data/lib/async/container/terminator.rb +33 -0
- data/lib/async/container/threaded.rb +3 -0
- data/lib/async/container/version.rb +1 -1
- data/spec/async/container/shared_examples.rb +12 -0
- data/spec/async/container_spec.rb +12 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3d08f68fa37d5145e4818a727e469dfd825434fab43decad9adfed7d052537b
|
4
|
+
data.tar.gz: d5bd93a2397f02b85c728f32ca2f9a73fe523c011cda18cd817d879851866d02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ef006b2a35082e55c394b404dc9cf067bf4282612199e7d3a5d10b1c9b604e5d208568c861e2089ba5bdfa5d9b232639d5b6af9cc4c86594a706e49898ce2ae
|
7
|
+
data.tar.gz: ebe0fe7a4429ad63635fbddcac7dca3ebfff7dbf87781b1c0d102dbef2370c7bbe148493e7677ad9168ba40258ec1559f3915053e19c7812eb88ef28734e1ab1
|
data/lib/async/container.rb
CHANGED
@@ -28,7 +28,7 @@ module Async
|
|
28
28
|
# Containers execute one or more "instances" which typically contain a reactor. A container spawns "instances" using threads and/or processes. Because these are resources that must be cleaned up some how (either by `join` or `waitpid`), their creation is deferred until the user invokes `Container#wait`. When executed this way, the container guarantees that all "instances" will be complete once `Container#wait` returns. Containers are constructs for achieving parallelism, and are not designed to be used directly for concurrency. Typically, you'd create one or more container, add some tasks to it, and then wait for it to complete.
|
29
29
|
module Container
|
30
30
|
def self.best_container_class
|
31
|
-
if Process.respond_to?(:fork) and Process.respond_to(:setpgid)
|
31
|
+
if Process.respond_to?(:fork) and Process.respond_to?(:setpgid)
|
32
32
|
return Forked
|
33
33
|
else
|
34
34
|
return Threaded
|
@@ -42,7 +42,7 @@ module Async
|
|
42
42
|
2
|
43
43
|
end
|
44
44
|
|
45
|
-
def self.new
|
45
|
+
def self.new(*arguments)
|
46
46
|
best_container_class.new(*arguments)
|
47
47
|
end
|
48
48
|
end
|
@@ -18,11 +18,29 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
+
require_relative 'terminator'
|
22
|
+
|
21
23
|
require 'async/reactor'
|
22
24
|
|
23
25
|
module Async
|
24
26
|
module Container
|
25
27
|
class Controller
|
28
|
+
def initialize
|
29
|
+
@attached = []
|
30
|
+
end
|
31
|
+
|
32
|
+
def attach(controller = nil, &block)
|
33
|
+
if controller
|
34
|
+
@attached << controller
|
35
|
+
end
|
36
|
+
|
37
|
+
if block_given?
|
38
|
+
@attached << Terminator.new(&block)
|
39
|
+
end
|
40
|
+
|
41
|
+
return self
|
42
|
+
end
|
43
|
+
|
26
44
|
def async(**options, &block)
|
27
45
|
spawn(**options) do |instance|
|
28
46
|
begin
|
@@ -40,6 +58,10 @@ module Async
|
|
40
58
|
|
41
59
|
return self
|
42
60
|
end
|
61
|
+
|
62
|
+
def stop(graceful = true)
|
63
|
+
@attached.each(&:stop)
|
64
|
+
end
|
43
65
|
end
|
44
66
|
end
|
45
67
|
end
|
@@ -49,6 +49,8 @@ module Async
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def initialize
|
52
|
+
super
|
53
|
+
|
52
54
|
@group = Group.new
|
53
55
|
@statistics = Statistics.new
|
54
56
|
end
|
@@ -91,6 +93,8 @@ module Async
|
|
91
93
|
|
92
94
|
# Gracefully shut down all children processes.
|
93
95
|
def stop(graceful = true)
|
96
|
+
super
|
97
|
+
|
94
98
|
@group.stop(graceful)
|
95
99
|
end
|
96
100
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Async
|
22
|
+
module Container
|
23
|
+
class Terminator
|
24
|
+
def initialize(&block)
|
25
|
+
@block = block
|
26
|
+
end
|
27
|
+
|
28
|
+
def stop(graceful = true)
|
29
|
+
@block.call(graceful)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -53,6 +53,8 @@ module Async
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def initialize
|
56
|
+
super
|
57
|
+
|
56
58
|
@threads = []
|
57
59
|
@running = true
|
58
60
|
@statistics = Statistics.new
|
@@ -106,6 +108,7 @@ module Async
|
|
106
108
|
# Gracefully shut down all reactors.
|
107
109
|
def stop(graceful = true)
|
108
110
|
@running = false
|
111
|
+
super
|
109
112
|
|
110
113
|
if graceful
|
111
114
|
@threads.each{|thread| thread.raise(Interrupt)}
|
@@ -21,6 +21,18 @@
|
|
21
21
|
require 'async/rspec/reactor'
|
22
22
|
|
23
23
|
RSpec.shared_examples_for Async::Container do
|
24
|
+
it "can attach terminator" do
|
25
|
+
terminated = false
|
26
|
+
|
27
|
+
subject.attach do
|
28
|
+
terminated = true
|
29
|
+
end
|
30
|
+
|
31
|
+
subject.stop
|
32
|
+
|
33
|
+
expect(terminated).to be_truthy
|
34
|
+
end
|
35
|
+
|
24
36
|
it "can run concurrently" do
|
25
37
|
input, output = IO.pipe
|
26
38
|
|
@@ -24,4 +24,16 @@ RSpec.describe Async::Container do
|
|
24
24
|
it "can get processor count" do
|
25
25
|
expect(Async::Container.processor_count).to be >= 1
|
26
26
|
end
|
27
|
+
|
28
|
+
it "can get best container class" do
|
29
|
+
expect(Async::Container.best_container_class).to_not be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
subject {Async::Container.new}
|
33
|
+
|
34
|
+
it "can get best container class" do
|
35
|
+
expect(subject).to_not be_nil
|
36
|
+
|
37
|
+
subject.stop
|
38
|
+
end
|
27
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-container
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: process-group
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- lib/async/container/group.rb
|
148
148
|
- lib/async/container/hybrid.rb
|
149
149
|
- lib/async/container/statistics.rb
|
150
|
+
- lib/async/container/terminator.rb
|
150
151
|
- lib/async/container/threaded.rb
|
151
152
|
- lib/async/container/version.rb
|
152
153
|
- spec/async/container/forked_spec.rb
|