worker_pool 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sebastian Ohm <ohm.sebastian@gmail.com>
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.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # WorkerPool
2
+
3
+ Playing around with synchronized concurrent task execution in Ruby.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib'
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,58 @@
1
+ require 'thread'
2
+
3
+ class WorkerPool
4
+ BUFFER_SIZE = 1
5
+
6
+ def initialize(worker_count)
7
+ @queue = SizedQueue.new(BUFFER_SIZE)
8
+ @workers = (1..worker_count).map { |i| start_worker(i) }
9
+ end
10
+
11
+ def perform(tasks, timeout, &block)
12
+ countdown, accumulator = CountdownLatch.new(tasks.size), Queue.new
13
+ tasks.each { |args| enqueue(countdown, accumulator, args, block) }
14
+ countdown.wait(timeout)
15
+ queue_to_array(accumulator)
16
+ end
17
+
18
+ def shutdown
19
+ @workers.each(&:kill)
20
+ end
21
+
22
+ private
23
+
24
+ def enqueue(countdown, accumulator, args, block)
25
+ @queue.push([ countdown, accumulator, args, block ])
26
+ end
27
+
28
+ def execute_task(countdown, accumulator, args, block)
29
+ result =
30
+ begin
31
+ block.call(*args)
32
+ rescue => ex
33
+ WorkerError.new(ex)
34
+ end
35
+ accumulator.push(result)
36
+ ensure
37
+ countdown.decrement
38
+ end
39
+
40
+ def queue_to_array(queue)
41
+ queue.length.times.map { queue.pop }
42
+ end
43
+
44
+ def start_worker(id)
45
+ Thread.new do
46
+ loop do
47
+ task = @queue.pop rescue nil
48
+ if Array === task
49
+ execute_task(*task)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ require 'worker_pool/countdown_latch'
57
+ require 'worker_pool/version'
58
+ require 'worker_pool/worker_error'
@@ -0,0 +1,29 @@
1
+ require 'thread'
2
+
3
+ class WorkerPool
4
+ class CountdownLatch
5
+ def initialize(count)
6
+ @count, @mutex, @resource = count, Mutex.new, ConditionVariable.new
7
+ end
8
+
9
+ def decrement
10
+ @mutex.synchronize do
11
+ @count -= 1
12
+ if @count <= 0
13
+ @resource.broadcast
14
+ end
15
+ end
16
+ end
17
+
18
+ # TODO: Implement timeouts
19
+ def wait(_timeout)
20
+ @mutex.synchronize do
21
+ if @count > 0
22
+ @resource.wait(@mutex)
23
+ else
24
+ @resource.broadcast
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ class WorkerPool
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,3 @@
1
+ class WorkerPool
2
+ class WorkerError < StandardError ; end
3
+ end
@@ -0,0 +1,12 @@
1
+ if ENV['COVERAGE']
2
+ begin
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ rescue LoadError
6
+ puts 'SimpleCov not loaded!'
7
+ end
8
+ end
9
+
10
+ require 'minitest/autorun'
11
+
12
+ Thread.abort_on_exception = true
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+ require 'worker_pool'
3
+
4
+ class WorkerPoolTest < MiniTest::Unit::TestCase
5
+ TIMEOUT = 5.0
6
+
7
+ def identity(arg)
8
+ arg
9
+ end
10
+
11
+ def sleepy_identity(arg)
12
+ sleep(rand(0.01..0.2))
13
+ arg
14
+ end
15
+
16
+ def fail(*args)
17
+ raise ArgumentError, args
18
+ end
19
+
20
+ def test_ordered_task_execution
21
+ pool, args, task = WorkerPool.new(1), (0..9).to_a, method(:identity)
22
+ results = pool.perform(args, TIMEOUT, &task)
23
+ pool.shutdown
24
+ assert_equal(args, results)
25
+ end
26
+
27
+ def test_concurrent_task_execution
28
+ pool, args, task = WorkerPool.new(2), (0..9).to_a, method(:sleepy_identity)
29
+ results = pool.perform(args, TIMEOUT, &task)
30
+ pool.shutdown
31
+ assert_equal(args.size, results.size)
32
+ args.each { |arg| assert(results.include?(arg)) }
33
+ end
34
+
35
+ def test_failing_task_execution
36
+ pool, args, task = WorkerPool.new(3), [ :a, :a, :a ], method(:fail)
37
+ results = pool.perform(args, TIMEOUT, &task)
38
+ pool.shutdown
39
+ assert_equal(args.size, results.size)
40
+ results.each { |r| assert(r.is_a?(WorkerPool::WorkerError)) }
41
+ assert_equal([ :a ].inspect, results.first.message)
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'worker_pool/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'worker_pool'
8
+ gem.version = WorkerPool::VERSION
9
+ gem.authors = [ 'Sebastian Ohm' ]
10
+ gem.email = [ 'ohm.sebastian@gmail.com' ]
11
+ gem.homepage = 'http://github.com/ohm/worker_pool'
12
+ gem.summary = 'Simple worker pool for (synchronized) concurrent task execution'
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.test_files = gem.files.grep(%r{\Atest/})
15
+ gem.require_paths = [ 'lib' ]
16
+ gem.add_development_dependency('simplecov')
17
+ gem.required_ruby_version = '>= 1.9'
18
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: worker_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Ohm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simplecov
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email:
32
+ - ohm.sebastian@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/worker_pool.rb
43
+ - lib/worker_pool/countdown_latch.rb
44
+ - lib/worker_pool/version.rb
45
+ - lib/worker_pool/worker_error.rb
46
+ - test/test_helper.rb
47
+ - test/worker_pool_test.rb
48
+ - worker_pool.gemspec
49
+ homepage: http://github.com/ohm/worker_pool
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '1.9'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.23
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Simple worker pool for (synchronized) concurrent task execution
73
+ test_files:
74
+ - test/test_helper.rb
75
+ - test/worker_pool_test.rb