thread_puddle 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.
Files changed (2) hide show
  1. data/lib/thread_puddle.rb +63 -0
  2. metadata +46 -0
@@ -0,0 +1,63 @@
1
+ require 'thread'
2
+
3
+ class ThreadPuddle
4
+ attr_reader :size
5
+
6
+ def initialize size = 24
7
+ @size = size
8
+ @queue = Queue.new
9
+ @pool = size.times.map do
10
+ Thread.new do
11
+ catch :exit do
12
+ loop do
13
+ job, args = queue.deq
14
+ job.call *args
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def submit *args, &block
22
+ queue.enq [block, args]
23
+ end
24
+
25
+ def shutdown timeout = nil
26
+ size.times do
27
+ submit do
28
+ throw :exit
29
+ end
30
+ end
31
+
32
+ if timeout
33
+ shutdown_with_timeout timeout
34
+ else
35
+ pool.each &:join
36
+ end
37
+
38
+ queue.clear
39
+ size = 0
40
+ end
41
+
42
+ def status
43
+ {
44
+ :size => size,
45
+ :threads => pool.map(&:status),
46
+ :queue => {
47
+ :size => queue.size,
48
+ :consumers => queue.num_waiting,
49
+ },
50
+ }
51
+ end
52
+
53
+ private
54
+ attr_reader :queue, :pool
55
+
56
+ def shutdown_with_timeout timeout
57
+ Timeout.timeout timeout.to_i do
58
+ pool.each &:join
59
+ end
60
+ rescue Timeout::Error
61
+ pool.each &:kill
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_puddle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dallas Marlow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: puddles of threads
15
+ email:
16
+ - dallasmarlow@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/thread_puddle.rb
22
+ homepage: https://github.com/dallasmarlow/thread_puddle
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: puddles of threads
46
+ test_files: []