actionpool 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,13 @@
1
+ 0.2.2
2
+ - Better thread management within the pool
3
+ - Restart thread timeouts when modified
4
+ - Fix argument passing
5
+ 0.2.1
6
+ - Set minimum pool size properly when initialized to avoid
7
+ creating extra threads (thanks to Roger Pack)
8
+ 0.2.0
9
+ - Added argument support for passed tasks (thanks simonmenke)
10
+ - Faster pool resizing
11
+ - Smarter thread creation to limit unneeded creation
12
+ - Allow floats for timeouts to provide better control
13
+ - Test verified support for 1.8.6, 1.8.7, 1.9.1 and JRuby 1.4.0RC1
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.rdoc ADDED
@@ -0,0 +1,192 @@
1
+ == ActionPool
2
+
3
+ ActionPool is just a simple thread pool. It allows for various constraints and resizing in a pretty easy and unobtrusive manner. You can set limits on how long tasks are worked on, as well as on the life of a thread. For things that like to use lots threads, it can be helpful to reuse threads instead of constantly recreating them.
4
+
5
+ === install (easy):
6
+
7
+ gem install ActionPool
8
+
9
+ === install (less easy):
10
+
11
+ git clone http://github.com/spox/actionpool.git
12
+ cd actionpool
13
+ gem build *.gemspec
14
+ gem install ./
15
+
16
+ === install (less easy that's a little easier)
17
+
18
+ {rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
19
+
20
+ == Documentation
21
+
22
+ {rdocs}[http://allgems.ruby-forum.com/gems/ActionPool/]
23
+
24
+ == Example
25
+
26
+ === Code:
27
+
28
+ require 'actionpool'
29
+
30
+ pool = ActionPool::Pool.new
31
+ pool.process do
32
+ sleep(2)
33
+ raise 'Wakeup main thread'
34
+ end
35
+ 20.times do
36
+ pool.process do
37
+ puts "Thread: #{Thread.current}"
38
+ sleep(0.1)
39
+ end
40
+ end
41
+ begin
42
+ sleep
43
+ rescue Exception => e
44
+ puts "Thread pool woke me up: #{e}"
45
+ end
46
+
47
+
48
+ === Result:
49
+
50
+ Thread: #<Thread:0x93ebeb8>
51
+ Thread: #<Thread:0x93eb92c>
52
+ Thread: #<Thread:0x93eb8a0>
53
+ Thread: #<Thread:0x93eb814>
54
+ Thread: #<Thread:0x93eb788>
55
+ Thread: #<Thread:0x93eb670>
56
+ Thread: #<Thread:0x93eb5e4>
57
+ Thread: #<Thread:0x93eb558>
58
+ Thread: #<Thread:0x93eb4cc>
59
+ Thread: #<Thread:0x93ebeb8>
60
+ Thread: #<Thread:0x93eb92c>
61
+ Thread: #<Thread:0x93eb8a0>
62
+ Thread: #<Thread:0x93eb814>
63
+ Thread: #<Thread:0x93eb788>
64
+ Thread: #<Thread:0x93eb670>
65
+ Thread: #<Thread:0x93eb5e4>
66
+ Thread: #<Thread:0x93eb558>
67
+ Thread: #<Thread:0x93eb4cc>
68
+ Thread: #<Thread:0x93eb92c>
69
+ Thread: #<Thread:0x93eb8a0>
70
+ Thread pool woke me up: Wakeup main thread
71
+
72
+ === Important note
73
+
74
+ The worker threads in the ActionPool will catch all Exception objects that your block fails to catch. Instead of just eating these exceptions, they are passed back to the creating thread of the pool (generally the main thread). This is important to note if you care about processing unexpected exceptions and what allows the example code above to be woken up from its sleep.
75
+
76
+ == The internals
77
+ ==== Probably more information than you really want
78
+
79
+ ActionPool has some simple settings that make things work. First, the pool has a minimum and maximum number of allowed threads. On initialization, the minimum number of threads are created and put into the pool. By default, this is 10 threads. As the number of tasks added to the pool increases, the pool will grow as needed. When more tasks are in the pool than threads to process them, new threads will be added into the pool, until the maximum thread threshold is reached. Taking the example above, we can demonstrate this easily by adjusting our limits:
80
+
81
+ require 'actionpool'
82
+
83
+ pool = ActionPool::Pool.new(:min_threads => 1, :max_threads => 3)
84
+ pool.process do
85
+ sleep(10)
86
+ raise 'Wakeup main thread'
87
+ end
88
+ 20.times do
89
+ pool.process do
90
+ puts "Thread: #{Thread.current}"
91
+ sleep(rand(0.0))
92
+ end
93
+ end
94
+ begin
95
+ sleep
96
+ rescue Exception => e
97
+ puts "Thread pool woke me up: #{e}"
98
+ end
99
+
100
+ Which results in:
101
+
102
+ Thread: #<Thread:0x86c1760>
103
+ Thread: #<Thread:0x86c1080>
104
+ Thread: #<Thread:0x86c1080>
105
+ Thread: #<Thread:0x86c1080>
106
+ Thread: #<Thread:0x86c1760>
107
+ Thread: #<Thread:0x86c1760>
108
+ Thread: #<Thread:0x86c1760>
109
+ Thread: #<Thread:0x86c1080>
110
+ Thread: #<Thread:0x86c1760>
111
+ Thread: #<Thread:0x86c1080>
112
+ Thread: #<Thread:0x86c1080>
113
+ Thread: #<Thread:0x86c1760>
114
+ Thread: #<Thread:0x86c1080>
115
+ Thread: #<Thread:0x86c1760>
116
+ Thread: #<Thread:0x86c1760>
117
+ Thread: #<Thread:0x86c1760>
118
+ Thread: #<Thread:0x86c1080>
119
+ Thread: #<Thread:0x86c1760>
120
+ Thread: #<Thread:0x86c1760>
121
+ Thread: #<Thread:0x86c1080>
122
+ Thread pool woke me up: Wakeup main thread
123
+
124
+ Our pool starts with a single thread that is occupied by the sleeping task waiting to raise an exception. As we begin to add new tasks, the pool grows to accommodate the growing number of tasks, until it reaches the maximum threshold of 3. At that point, the pool simply processes the tasks until the task list is empty.
125
+
126
+ The pool also has the ability to limit the amount of time a thread spends working on a given task. By default, a thread will work on a given task until the task is completed, or the pool is shutdown. However, as the following example shows, it is very easy to limit this time to avoid the pool being bogged down on long running tasks:
127
+
128
+ require 'actionpool'
129
+
130
+ pool = ActionPool::Pool.new(:min_threads => 1, :max_threads => 1, :a_to => 1)
131
+ pool.process do
132
+ puts "#{Time.now}: I'm a long running task"
133
+ sleep(100)
134
+ raise 'Wakeup main thread'
135
+ end
136
+ pool.process do
137
+ puts "#{Time.now}: Waiting for my turn"
138
+ raise "I'm waking up the main thread"
139
+ end
140
+ begin
141
+ sleep
142
+ rescue Exception => e
143
+ puts "Thread pool woke me up: #{e}"
144
+ end
145
+
146
+ Results:
147
+
148
+ 2009-10-10 08:47:08 -0700: I'm a long running task
149
+ 2009-10-10 08:47:09 -0700: Waiting for my turn
150
+ Thread pool woke me up: I'm waking up the main thread
151
+
152
+ If you have a number of tasks you would like to schedule at once, it is easy with the add_jobs method:
153
+
154
+ require 'actionpool'
155
+
156
+ pool = ActionPool::Pool.new
157
+ a = 0
158
+ lock = Mutex.new
159
+ tasks = [].fill(lambda{ lock.synchronize{ a += 1 } }, 0..19)
160
+ pool.add_jobs(tasks)
161
+ pool.shutdown
162
+ puts "Result: #{a}"
163
+
164
+ Results:
165
+
166
+ Result: 20
167
+
168
+ Passing arguments to tasks is now available as well:
169
+
170
+ require 'actionpool'
171
+
172
+ pool = ActionPool::Pool.new
173
+ string = 'Hello world'
174
+ puts "Original: #{string}. ID: #{string.object_id}"
175
+ pool << [lambda{|var| puts "Passed: #{var}. ID: #{var.object_id}"}, [string.dup]]
176
+ pool << [lambda{|a,b| puts "Passed: #{a} | #{b}. ID: #{a.object_id} | #{b.object_id}"}, [string, string.dup]]
177
+ pool.shutdown
178
+
179
+ Results:
180
+
181
+ Original: Hello world. ID: 70651630
182
+ Passed: Hello world. ID: 70651250
183
+ Passed: Hello world | Hello world. ID: 70651630 | 70651100
184
+
185
+ == Last remarks
186
+
187
+ If you find any bugs, please report them through {github}[http://github.com/spox/actionpool/issues]. If you are in need of any help, you can generally find me on DALnet and Freenode.
188
+
189
+ == License
190
+
191
+ ActionPool is licensed under the LGPLv3
192
+ Copyright (c) 2009 spox <spox@modspox.com>
data/Rakefile ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |s|
7
+ s.name = 'ActionPool'
8
+ s.author = %q(spox)
9
+ s.email = %q(spox@modspox.com)
10
+ s.version = '0.0.1'
11
+ s.summary = %q(Thread Pooling Library)
12
+ s.platform = Gem::Platform::RUBY
13
+ s.files = Dir['**/*']
14
+ s.rdoc_options = %w(--title ActionPool --main README.rdoc --line-numbers)
15
+ s.extra_rdoc_files = %w(README.rdoc)
16
+ s.require_paths = %w(lib)
17
+ s.required_ruby_version = '>= 1.8.6'
18
+ s.rubyforge_project = 'ActionPool'
19
+ s.add_development_dependency 'thoughtbot-shoulda'
20
+ s.homepage = %q(http://dev.modspox.com/trac/ActionPool)
21
+ description = []
22
+ File.open("README.rdoc") do |file|
23
+ file.each do |line|
24
+ line.chomp!
25
+ break if line.empty?
26
+ description << "#{line.gsub(/\[\d\]/, '')}"
27
+ end
28
+ end
29
+ s.description = description[1..-1].join(" ")
30
+ end
31
+ Jeweler::RubyforgeTasks.new do |rubyforge|
32
+ rubyforge.doc_task = 'rdoc'
33
+ end
34
+ rescue LoadError
35
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
36
+ end
37
+
38
+ require 'rake/testtask'
39
+ Rake::TestTask.new(:test) do |test|
40
+ test.libs << 'lib' << 'test'
41
+ test.pattern = 'test/**/*_test.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ begin
46
+ require 'rcov/rcovtask'
47
+ Rcov::RcovTask.new do |test|
48
+ test.libs << 'test'
49
+ test.pattern = 'test/**/*_test.rb'
50
+ test.verbose = true
51
+ end
52
+ rescue LoadError
53
+ task :rcov do
54
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
55
+ end
56
+ end
57
+
58
+ task :test => :check_dependencies
59
+
60
+ task :default => :test
61
+
62
+ require 'rake/rdoctask'
63
+ Rake::RDocTask.new do |rdoc|
64
+ if File.exist?('VERSION')
65
+ version = File.read('VERSION')
66
+ else
67
+ version = ""
68
+ end
69
+
70
+ rdoc.rdoc_dir = 'rdoc'
71
+ rdoc.title = "test_repo #{version}"
72
+ rdoc.rdoc_files.include('README*')
73
+ rdoc.rdoc_files.include('lib/**/*.rb')
74
+ end
@@ -0,0 +1,15 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'actionpool'
3
+ s.author = %q(spox)
4
+ s.email = %q(spox@modspox.com)
5
+ s.version = '0.2.2'
6
+ s.summary = %q(Thread Pool)
7
+ s.platform = Gem::Platform::RUBY
8
+ s.files = Dir['**/*']
9
+ s.rdoc_options = %w(--title ActionPool --main README.rdoc --line-numbers)
10
+ s.extra_rdoc_files = %w(README.rdoc CHANGELOG)
11
+ s.require_paths = %w(lib)
12
+ s.required_ruby_version = '>= 1.8.6'
13
+ s.homepage = %q(http://github.com/spox/actionpool)
14
+ s.description = "The ActionPool is an easy to use thread pool for ruby."
15
+ end
@@ -0,0 +1,25 @@
1
+ module ActionPool
2
+ class LogHelper
3
+
4
+ def initialize(logger=nil)
5
+ require 'logger'
6
+ @logger = logger
7
+ end
8
+
9
+ def info(m)
10
+ @logger.info(m) unless @logger.nil?
11
+ end
12
+
13
+ def warn(m)
14
+ @logger.warn(m) unless @logger.nil?
15
+ end
16
+
17
+ def fatal(m)
18
+ @logger.fatal(m) unless @logger.nil?
19
+ end
20
+
21
+ def error(m)
22
+ @logger.error(m) unless @logger.nil?
23
+ end
24
+ end
25
+ end