fizx-thread_pool 0.1.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.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 0.1.0
2
+ - Initial release
data/README ADDED
@@ -0,0 +1,9 @@
1
+ A simple executor-style ThreadPool for Ruby (with tests, yay!)
2
+
3
+ Usage:
4
+
5
+ require "rubygems"
6
+ require "thread_pool"
7
+ pool = ThreadPool.new(threads = 10)
8
+ pool.execute { puts "I'm writing from a thread" }
9
+ pool.join
@@ -0,0 +1,57 @@
1
+ # Hooray
2
+ class ThreadPool
3
+ class Executor
4
+ attr_reader :active
5
+
6
+ def initialize(queue)
7
+ @thread = Thread.new do
8
+ loop do
9
+ if block = queue.shift
10
+ @active = true
11
+ block.call
12
+ else
13
+ @active = false
14
+ sleep 0.01
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def close
21
+ @thread.exit
22
+ end
23
+ end
24
+
25
+ # Initialize with number of threads to run
26
+ def initialize(count)
27
+ @executors = []
28
+ @queue = []
29
+ @count = count
30
+ count.times { @executors << Executor.new(@queue) }
31
+ end
32
+
33
+ # Runs the block at some time in the near future
34
+ def execute(&block)
35
+ @queue << block
36
+ end
37
+
38
+ # Size of the task queue
39
+ def waiting
40
+ @queue.size
41
+ end
42
+
43
+ # Size of the thread pool
44
+ def size
45
+ @count
46
+ end
47
+
48
+ # Kills all threads
49
+ def close
50
+ @executors.each {|e| e.close }
51
+ end
52
+
53
+ # Sleeps and blocks until the task queue is finished executing
54
+ def join
55
+ sleep 0.01 until @queue.empty? && @executors.all?{|e| !e.active}
56
+ end
57
+ end
@@ -0,0 +1,44 @@
1
+ require "test/unit"
2
+ require "timeout"
3
+ require File.dirname(__FILE__) + "/../lib/thread_pool"
4
+
5
+ class TestThreadPool < Test::Unit::TestCase
6
+ THREADS = 10
7
+
8
+ def setup
9
+ @pool = ThreadPool.new(THREADS)
10
+ end
11
+
12
+ def teardown
13
+ @pool.close
14
+ end
15
+
16
+ def test_creation
17
+ #implicit
18
+ end
19
+
20
+ def test_pool_size
21
+ assert_equal THREADS, @pool.size
22
+ end
23
+
24
+ def test_waiting
25
+ n = 50
26
+ n.times {
27
+ @pool.execute { sleep 10 }
28
+ }
29
+ sleep 0.01
30
+ assert_equal n - THREADS, @pool.waiting
31
+ end
32
+
33
+ def test_execution
34
+ Timeout::timeout(1) do
35
+ n = 50
36
+ @foo = []
37
+ n.times {
38
+ @pool.execute { @foo << "hi" }
39
+ }
40
+ @pool.join
41
+ assert_equal n, @foo.length
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fizx-thread_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Maxwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple thread pool
17
+ email: kyle@kylemaxwell.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - README
25
+ files:
26
+ - README
27
+ - CHANGELOG
28
+ - lib/thread_pool.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/fizx/thread_pool
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --main
34
+ - README
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: A ruby thread pool
56
+ test_files:
57
+ - test/test_thread_pool.rb