parapool 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebb82a4c9960fd87ef25f787458cefdce08e37a6
4
+ data.tar.gz: 392810561aa2560fcc39e919a1f33a8715175757
5
+ SHA512:
6
+ metadata.gz: 347bd61fcbd6cc3c0565fbae831ac23563be067e43db24c2c00bbffc9568873531ff7c6389f8b9f6d114c5438bdef55affa1524b7b5a6ff706405790f931a074
7
+ data.tar.gz: 67ccdbf4f6d56fd072a921cb9c687f9144846395bd5a9068a76cfd0742c2641208c3e2d3a3ee9cefb6465b478d20ec72f36fa6dc70ed7fe4a4f795f7005c378b
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parapool.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tomato Ketchup
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Parapool
2
+
3
+ Provides parallel processing on thread pool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'parapool'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install parapool
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ pool = Parapool.new
25
+ pool.map([1, 2, 3, 4, 5]).map do |data|
26
+ data ** 2
27
+ end
28
+ # => [1, 4, 9, 16, 25]
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/kechako/parapool/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,67 @@
1
+ require 'parapool/error'
2
+ require 'parapool/job'
3
+ require 'parapool/synchronizer'
4
+ require 'parapool/version'
5
+ require 'parapool/worker'
6
+ require 'thread'
7
+
8
+ class Parapool
9
+ attr_reader :pool_size
10
+
11
+ def initialize(pool_size = 4)
12
+ raise ArgumentError, 'Pool size must be greater than or equal to 1.' if pool_size < 1
13
+
14
+ @pool_size = pool_size
15
+ @queue = Queue.new
16
+ @workers = []
17
+
18
+ create_worker
19
+ end
20
+
21
+ def map(params, &block)
22
+ raise TypeError, "wrong argument type #{params.class} (expected Enumerable)" unless params.is_a?(Enumerable)
23
+ raise Parapool::Error, 'must be called with a block' unless block_given?
24
+
25
+ return if params.empty?
26
+
27
+ sync = Synchronizer.new(params.size)
28
+
29
+ jobs = []
30
+ params.each do |param|
31
+ job = Job.new(param, sync, &block)
32
+ push(job)
33
+ jobs << job
34
+ end
35
+
36
+ sync.wait
37
+
38
+ jobs.map { |job| job.result }
39
+ end
40
+
41
+ def release
42
+ @workers.size.times.each do
43
+ @queue.push(nil)
44
+ end
45
+ @workers.each do |worker|
46
+ worker.join
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def create_worker
53
+ @pool_size.times do
54
+ worker = Worker.new(@queue)
55
+ worker.run
56
+ @workers << worker
57
+ end
58
+ end
59
+
60
+ def push(job)
61
+ raise TypeError, "wrong argument type #{job.class} (expected Parapool::Job)" unless job.is_a?(Job)
62
+
63
+ @queue.push(job)
64
+
65
+ self
66
+ end
67
+ end
@@ -0,0 +1,4 @@
1
+ class Parapool
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ require 'parapool/error'
2
+
3
+ class Parapool
4
+ class Job
5
+ attr_accessor :param, :result
6
+
7
+ def initialize(param, sync, &block)
8
+ raise Parapool::Error, 'must be called with a block' unless block_given?
9
+
10
+ @param = param
11
+ @sync = sync
12
+ @block = block
13
+ end
14
+
15
+ def run
16
+ @result = @block.call(param) rescue $!
17
+
18
+ @sync.count
19
+
20
+ @result
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ require 'monitor'
2
+
3
+ class Parapool
4
+ class Synchronizer
5
+ include MonitorMixin
6
+
7
+ def initialize(count)
8
+ super()
9
+
10
+ @count = count
11
+
12
+ @completed = new_cond
13
+ end
14
+
15
+ def count
16
+ synchronize do
17
+ @count -= 1
18
+
19
+ @completed.broadcast
20
+ end
21
+
22
+ self
23
+ end
24
+
25
+ def wait
26
+ synchronize do
27
+ @completed.wait_until { @count.zero? }
28
+ end
29
+
30
+ self
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ class Parapool
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'thread'
2
+
3
+ class Parapool
4
+ class Worker
5
+ def initialize(queue)
6
+ raise TypeError, "wrong argument type #{queue.class} (expected Queue)" unless queue.is_a?(Queue)
7
+
8
+ @queue = queue
9
+ end
10
+
11
+ def run
12
+ @thread = Thread.new do
13
+ while job = @queue.pop do
14
+ job.run
15
+ end
16
+ end
17
+
18
+ self
19
+ end
20
+
21
+ def join
22
+ @thread.join
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parapool/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parapool"
8
+ spec.version = Parapool::VERSION
9
+ spec.authors = ["Tomato Ketchup"]
10
+ spec.email = ["r@554.jp"]
11
+ spec.summary = 'Provides parallel processing on thread pool.'
12
+ spec.description = 'Provides parallel processing on thread pool.'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-doc"
26
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parapool do
4
+ it 'has a version number' do
5
+ expect(Parapool::VERSION).not_to be nil
6
+ end
7
+
8
+ context 'when create instance without pool size' do
9
+ let(:pool) { Parapool.new }
10
+
11
+ it 'must be able to get pool size' do
12
+ expect(pool.pool_size).to eq(4)
13
+ end
14
+ end
15
+
16
+ context 'when create instance with pool size' do
17
+ let(:pool) { Parapool.new(10) }
18
+
19
+ it 'must be able to get pool size' do
20
+ expect(pool.pool_size).to eq(10)
21
+ end
22
+ end
23
+
24
+ context 'when run tasks in Parapool' do
25
+ let(:pool) { Parapool.new }
26
+ let(:params) { (1..10).to_a }
27
+
28
+ before do
29
+ @results = pool.map(params) { |num| num * 2 }
30
+ end
31
+
32
+ it 'must be able to get results' do
33
+ expect(@results).to eq(params.map { |num| num * 2 })
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'parapool'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parapool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomato Ketchup
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-doc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Provides parallel processing on thread pool.
84
+ email:
85
+ - r@554.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/parapool.rb
98
+ - lib/parapool/error.rb
99
+ - lib/parapool/job.rb
100
+ - lib/parapool/synchronizer.rb
101
+ - lib/parapool/version.rb
102
+ - lib/parapool/worker.rb
103
+ - parapool.gemspec
104
+ - spec/parapool_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Provides parallel processing on thread pool.
130
+ test_files:
131
+ - spec/parapool_spec.rb
132
+ - spec/spec_helper.rb
133
+ has_rdoc: