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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d13b298818cc220c0170d9176d35babca39b807
4
- data.tar.gz: 13c70be836baf4aff7b2fcbdfccded092e272002
3
+ metadata.gz: 0113af6fb911302493392efa436bd557868e34a0
4
+ data.tar.gz: c8c215e965f2c8793ce42ca92650e30f28127cfa
5
5
  SHA512:
6
- metadata.gz: 9fa10e69f20e1a9153aaa38d92c19b05d879b9b5dafe06abe727c09ef6f48efa1e2d424eca508672efb72c91f414f9fa70f1bea30d99ce2c271db88c83031630
7
- data.tar.gz: a1652049cdc38454d220778f793ef0cf65f6167338d6a12b94e5875ef67ff13535fe1b107c4652d7058c8bf24823bf461f78877e1334b1687220ea4e48484a77
6
+ metadata.gz: 208a75436dc546580844be7d284328f1828ed4654cfac6cf53df9000ef609d0cfdf9713f3f1253bfa6e6cde2e2f250e90f54006267c3f7b75e940e7d0880c38d
7
+ data.tar.gz: a8eead8fba97d5406e5c1021f1c4c7400a3ceb89355431b82047f37ee3a8f939613447bc0d7a222f08e6917f1a3f4e5540cc7dba7f47426584e9832c71c277fd
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Infrastruct
2
+ [![Build Status](https://travis-ci.org/smolnar/infrastruct.svg?branch=master)](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.run # => 1 ^ 2 + 2 ^ 2 + 3 ^ 2 = 14
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
@@ -2,33 +2,18 @@ module Infrastruct
2
2
  class Manager
3
3
  attr_reader :queue
4
4
 
5
- def initialize(worker_factory, thread_pool:)
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
- def enqueue(*args, &block)
13
- @thread_pool.enqueue([args, block])
8
+ @thread_pool.run
14
9
  end
15
10
 
16
- def run
17
- @thread_pool.run do |params|
18
- begin
19
- worker = @worker_factory.new
20
- result = worker.perform(*params[0], &params[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
- worker = @worker_factory.new
31
- worker.collect(@results)
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
- attr_reader :queue
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 run(&block)
15
- @threads = @number_of_threads.times.map do
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 params = @queue.pop do
19
- block.call(params)
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
@@ -1,3 +1,3 @@
1
1
  module Infrastruct
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
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/nonblocking_queue'
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
- pool = Infrastruct::ThreadPool.new(threads: options[:threads])
10
+ runner = Infrastruct::Runner.new(self)
11
+ pool = Infrastruct::ThreadPool.new(runner, threads: options[:threads])
10
12
 
11
- Manager.new(self, thread_pool: pool)
13
+ Manager.new(pool)
12
14
  end
13
15
 
14
16
  def options(options = {})
15
- @options ||= { threads: 5 }
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.1.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-04 00:00:00.000000000 Z
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/nonblocking_queue.rb
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
@@ -1,15 +0,0 @@
1
- module Infrastruct
2
- class NonblockingQueue
3
- def initialize
4
- @queue = ::Queue.new
5
- end
6
-
7
- def push(element)
8
- @queue.push(element)
9
- end
10
-
11
- def pop
12
- @queue.pop(true)
13
- end
14
- end
15
- end