infrastruct 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +2 -1
- data/lib/infrastruct/blocking_queue.rb +25 -0
- data/lib/infrastruct/manager.rb +7 -22
- data/lib/infrastruct/runner.rb +19 -0
- data/lib/infrastruct/thread_pool.rb +22 -10
- data/lib/infrastruct/version.rb +1 -1
- data/lib/infrastruct.rb +6 -4
- metadata +4 -3
- data/lib/infrastruct/nonblocking_queue.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0113af6fb911302493392efa436bd557868e34a0
|
4
|
+
data.tar.gz: c8c215e965f2c8793ce42ca92650e30f28127cfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 208a75436dc546580844be7d284328f1828ed4654cfac6cf53df9000ef609d0cfdf9713f3f1253bfa6e6cde2e2f250e90f54006267c3f7b75e940e7d0880c38d
|
7
|
+
data.tar.gz: a8eead8fba97d5406e5c1021f1c4c7400a3ceb89355431b82047f37ee3a8f939613447bc0d7a222f08e6917f1a3f4e5540cc7dba7f47426584e9832c71c277fd
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Infrastruct
|
2
|
+
[](https://travis-ci.org/smolnar/infrastruct)
|
2
3
|
|
3
4
|
Process and distribute asynchronous tasks and merge their results.
|
4
5
|
|
@@ -53,7 +54,7 @@ worker.enqueue(3)
|
|
53
54
|
Run worker and wait for merge results.
|
54
55
|
|
55
56
|
```
|
56
|
-
sum = worker.
|
57
|
+
sum = worker.result # => 1 ^ 2 + 2 ^ 2 + 3 ^ 2 = 14
|
57
58
|
```
|
58
59
|
|
59
60
|
## Development
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Infrastruct
|
2
|
+
class BlockingQueue
|
3
|
+
def initialize
|
4
|
+
@queue = ::Queue.new
|
5
|
+
@mutex = Mutex.new
|
6
|
+
@blocking = true
|
7
|
+
end
|
8
|
+
|
9
|
+
def push(element)
|
10
|
+
@queue.push(element)
|
11
|
+
end
|
12
|
+
|
13
|
+
def pop
|
14
|
+
@mutex.synchronize do
|
15
|
+
@queue.pop(!@blocking)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def unblock!
|
20
|
+
@mutex.synchronize do
|
21
|
+
@blocking = false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/infrastruct/manager.rb
CHANGED
@@ -2,33 +2,18 @@ module Infrastruct
|
|
2
2
|
class Manager
|
3
3
|
attr_reader :queue
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@worker_factory = worker_factory
|
5
|
+
def initialize(thread_pool)
|
7
6
|
@thread_pool = thread_pool
|
8
|
-
@results = Array.new
|
9
|
-
@mutex = Mutex.new
|
10
|
-
end
|
11
7
|
|
12
|
-
|
13
|
-
@thread_pool.enqueue([args, block])
|
8
|
+
@thread_pool.run
|
14
9
|
end
|
15
10
|
|
16
|
-
def
|
17
|
-
@thread_pool.
|
18
|
-
|
19
|
-
worker = @worker_factory.new
|
20
|
-
result = worker.perform(*params[0], ¶ms[1])
|
21
|
-
|
22
|
-
@mutex.synchronize do
|
23
|
-
@results.push(result)
|
24
|
-
end
|
25
|
-
rescue
|
26
|
-
@thread_pool.enqueue(params)
|
27
|
-
end
|
28
|
-
end
|
11
|
+
def enqueue(*args)
|
12
|
+
@thread_pool.enqueue(args)
|
13
|
+
end
|
29
14
|
|
30
|
-
|
31
|
-
|
15
|
+
def result
|
16
|
+
@thread_pool.finalize
|
32
17
|
end
|
33
18
|
end
|
34
19
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Infrastruct
|
2
|
+
class Runner
|
3
|
+
def initialize(factory)
|
4
|
+
@factory = factory
|
5
|
+
end
|
6
|
+
|
7
|
+
def perform(args)
|
8
|
+
worker = @factory.new
|
9
|
+
|
10
|
+
worker.perform(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def collect(results)
|
14
|
+
worker = @factory.new
|
15
|
+
|
16
|
+
worker.collect(results)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,30 +1,42 @@
|
|
1
1
|
module Infrastruct
|
2
2
|
class ThreadPool
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(threads:)
|
6
|
-
@queue = Infrastruct::NonblockingQueue.new
|
3
|
+
def initialize(runner, threads:)
|
4
|
+
@runner = runner
|
7
5
|
@number_of_threads = threads
|
6
|
+
@queue = Infrastruct::BlockingQueue.new
|
7
|
+
@results = Array.new
|
8
|
+
@threads = []
|
8
9
|
end
|
9
10
|
|
10
11
|
def enqueue(args)
|
11
12
|
@queue.push(args)
|
12
13
|
end
|
13
14
|
|
14
|
-
def
|
15
|
-
@
|
15
|
+
def finalize
|
16
|
+
@queue.unblock!
|
17
|
+
@threads.map(&:join)
|
18
|
+
|
19
|
+
@runner.collect(@results)
|
20
|
+
end
|
21
|
+
|
22
|
+
def run
|
23
|
+
mutex = Mutex.new
|
24
|
+
|
25
|
+
@threads = @number_of_threads.times.map do |n|
|
16
26
|
Thread.new do
|
17
27
|
begin
|
18
|
-
while
|
19
|
-
|
28
|
+
while args = @queue.pop do
|
29
|
+
result = @runner.perform(args)
|
30
|
+
|
31
|
+
mutex.synchronize do
|
32
|
+
@results << result
|
33
|
+
end
|
20
34
|
end
|
21
35
|
rescue ThreadError => error
|
22
36
|
raise error unless error.message == 'queue empty'
|
23
37
|
end
|
24
38
|
end
|
25
39
|
end
|
26
|
-
|
27
|
-
@threads.map(&:join)
|
28
40
|
end
|
29
41
|
end
|
30
42
|
end
|
data/lib/infrastruct/version.rb
CHANGED
data/lib/infrastruct.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
require 'thread'
|
2
2
|
require 'infrastruct/version'
|
3
3
|
require 'infrastruct/manager'
|
4
|
-
require 'infrastruct/
|
4
|
+
require 'infrastruct/runner'
|
5
|
+
require 'infrastruct/blocking_queue'
|
5
6
|
require 'infrastruct/thread_pool'
|
6
7
|
|
7
8
|
module Infrastruct
|
8
9
|
def create
|
9
|
-
|
10
|
+
runner = Infrastruct::Runner.new(self)
|
11
|
+
pool = Infrastruct::ThreadPool.new(runner, threads: options[:threads])
|
10
12
|
|
11
|
-
Manager.new(
|
13
|
+
Manager.new(pool)
|
12
14
|
end
|
13
15
|
|
14
16
|
def options(options = {})
|
15
|
-
@options ||= { threads:
|
17
|
+
@options ||= { threads: 25 }
|
16
18
|
|
17
19
|
@options.merge(options)
|
18
20
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infrastruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Molnar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,8 +84,9 @@ files:
|
|
84
84
|
- bin/setup
|
85
85
|
- infrastruct.gemspec
|
86
86
|
- lib/infrastruct.rb
|
87
|
+
- lib/infrastruct/blocking_queue.rb
|
87
88
|
- lib/infrastruct/manager.rb
|
88
|
-
- lib/infrastruct/
|
89
|
+
- lib/infrastruct/runner.rb
|
89
90
|
- lib/infrastruct/thread_pool.rb
|
90
91
|
- lib/infrastruct/version.rb
|
91
92
|
homepage: https://github.com/smolnar/infrastruct
|