tpool 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (>= 1.0.0)
29
+ jeweler (~> 1.8.4)
30
+ rdoc (~> 3.12)
31
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kasper Johansen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = tpool
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to tpool
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Kasper Johansen. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tpool"
18
+ gem.homepage = "http://github.com/kaspernj/tpool"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A thread-pool for Ruby.}
21
+ gem.description = %Q{A thread-pool for Ruby that supports joining, status-checking, stopping jobs and more.}
22
+ gem.email = "k@spernj.org"
23
+ gem.authors = ["Kasper Johansen"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "tpool #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/tpool.rb ADDED
@@ -0,0 +1,96 @@
1
+ require "thread"
2
+
3
+ class Tpool
4
+ def self.const_missing(name)
5
+ require "#{File.dirname(__FILE__)}/tpool_#{name.to_s.downcase}.rb"
6
+ raise "Still not defined: '#{name}'." if !Tpool.const_defined?(name)
7
+ return Tpool.const_get(name)
8
+ end
9
+
10
+ #Returns the 'Tpool::Block' that is running in the given thread.
11
+ def self.current_block(thread = Thread.current)
12
+ return thread[:tpool][:block] if thread[:tpool].is_a?(Hash)
13
+ raise "No block was found running in that thread."
14
+ end
15
+
16
+ def initialize(args)
17
+ @args = args
18
+ @queue = Queue.new
19
+ self.start
20
+ end
21
+
22
+ def start
23
+ raise "Already started." if @pool and !@pool.empty?
24
+
25
+ @pool = Array.new(@args[:threads]) do |i|
26
+ Thread.new do
27
+ begin
28
+ Thread.current[:tpool] = {:i => i}
29
+
30
+ loop do
31
+ block = @queue.pop
32
+ Thread.current[:tpool][:block] = block
33
+
34
+ block.run
35
+ Thread.current[:tpool].delete(:block)
36
+ end
37
+ rescue Exception => e
38
+ $stderr.puts e.inspect
39
+ $stderr.puts e.backtrace
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ #Kills all running threads.
46
+ def stop
47
+ @pool.delete_if do |thread|
48
+ thread.kill
49
+ true
50
+ end
51
+ end
52
+
53
+ #Runs the given block in the thread-pool, joins it and returns the result.
54
+ def run(*args, &blk)
55
+ raise "No block was given." if !blk
56
+
57
+ block = Tpool::Block.new(
58
+ :args => args,
59
+ :blk => blk,
60
+ :thread_starts => [Thread.current]
61
+ )
62
+ @queue << block
63
+
64
+ begin
65
+ Thread.stop
66
+ rescue Exception
67
+ #Its not possible to stop main thread (dead-lock-error - sleep it instead).
68
+ sleep 0.1 while !block.done?
69
+ end
70
+
71
+ return block.res
72
+ end
73
+
74
+ #Runs the given block in the thread-pool and returns a block-object to keep a look on the status.
75
+ def run_async(*args, &blk)
76
+ raise "No block was given." if !blk
77
+
78
+ block = Tpool::Block.new(
79
+ :tpool => self,
80
+ :args => args,
81
+ :blk => blk,
82
+ :thread_starts => [Thread.current]
83
+ )
84
+ @queue << block
85
+
86
+ return block
87
+ end
88
+
89
+ def on_error_call(*args)
90
+ @on_error.call(*args) if @on_error
91
+ end
92
+
93
+ def on_error(&blk)
94
+ @on_error = blk
95
+ end
96
+ end
@@ -0,0 +1,90 @@
1
+ class Tpool::Block
2
+ attr_accessor :res
3
+
4
+ #Constructor. Should not be called manually.
5
+ def initialize(args)
6
+ @args = args
7
+
8
+ @running = false
9
+ @done = false
10
+ @error = nil
11
+ end
12
+
13
+ #Starts running whatever block it is holding.
14
+ def run
15
+ @thread_running = Thread.current
16
+ @running = true
17
+
18
+ begin
19
+ @res = @args[:blk].call(*@args[:args], &@args[:blk])
20
+ rescue Exception => e
21
+ @error = e
22
+ @args[:tpool].on_error_call(:on_error, e)
23
+ ensure
24
+ @running = false
25
+ @done = true
26
+ @thread_running = nil
27
+ end
28
+
29
+ if @args[:thread_starts]
30
+ @args[:thread_starts].each do |thread|
31
+ thread.wakeup
32
+ end
33
+ end
34
+
35
+ return self
36
+ end
37
+
38
+ #Returns true if the asynced job is done running.
39
+ def done?
40
+ return @done
41
+ end
42
+
43
+ #Returns true if the asynced job is still running.
44
+ def running?
45
+ return @running
46
+ end
47
+
48
+ #Returns true if the asynced job is still waiting to run.
49
+ def waiting?
50
+ return true if !@done and !@running and !@error
51
+ return false
52
+ end
53
+
54
+ #Raises error if one has happened in the asynced job.
55
+ def error!
56
+ #Wait for error to get set if any.
57
+ self.join
58
+
59
+ #Raise it if it got set.
60
+ raise @error if @error
61
+ end
62
+
63
+ #Sleeps until the asynced job is done. If an error occurred in the job, that error will be raised when calling the method.
64
+ def join
65
+ if !@done
66
+ @args[:thread_starts] << Thread.current
67
+
68
+ begin
69
+ Thread.stop
70
+ rescue Exception
71
+ sleep 0.1 while !@done
72
+ end
73
+ end
74
+
75
+ return self
76
+ end
77
+
78
+ #Kills the current running job.
79
+ def kill
80
+ Thread.pass while !self.done? and !self.running?
81
+ @thread_running.raise Exception, "Should kill itself." if !self.done? and self.running?
82
+ end
83
+
84
+ def result(args = nil)
85
+ self.join if args and args[:wait]
86
+ raise "Not done yet." unless self.done?
87
+ self.error!
88
+ return @res
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'tpool'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Tpool" do
4
+ it "should work" do
5
+ tp = Tpool.new(:threads => 4)
6
+ res = tp.run do
7
+ "kasper"
8
+ end
9
+
10
+ raise "Expected result to be 'kasper' but it wasnt: '#{res}'." if res != "kasper"
11
+
12
+
13
+ blk = tp.run_async do
14
+ "kasper"
15
+ end
16
+ blk.join
17
+
18
+ raise "Expected result to be 'kasper' but it wasnt: '#{res}'." if res != "kasper"
19
+
20
+
21
+
22
+ blk = tp.run_async do
23
+ sleep 5
24
+ "kasper"
25
+ end
26
+
27
+ blk.kill
28
+
29
+ error = false
30
+ begin
31
+ blk.error!
32
+ rescue Exception
33
+ error = true
34
+ end
35
+
36
+ raise "Expected error but didnt get one." if !error
37
+ end
38
+ end
data/tpool.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tpool}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kasper Johansen"]
12
+ s.date = %q{2012-08-19}
13
+ s.description = %q{A thread-pool for Ruby that supports joining, status-checking, stopping jobs and more.}
14
+ s.email = %q{k@spernj.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/tpool.rb",
29
+ "lib/tpool_block.rb",
30
+ "spec/spec_helper.rb",
31
+ "spec/tpool_spec.rb",
32
+ "tpool.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/kaspernj/tpool}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{A thread-pool for Ruby.}
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
45
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
46
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
59
+ end
60
+ end
61
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpool
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Kasper Johansen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-08-19 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.8.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "3.12"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.8.4
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: A thread-pool for Ruby that supports joining, status-checking, stopping jobs and more.
61
+ email: k@spernj.org
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/tpool.rb
79
+ - lib/tpool_block.rb
80
+ - spec/spec_helper.rb
81
+ - spec/tpool_spec.rb
82
+ - tpool.gemspec
83
+ has_rdoc: true
84
+ homepage: http://github.com/kaspernj/tpool
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 733002826955008460
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.2
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A thread-pool for Ruby.
114
+ test_files: []
115
+