Class: ThreadKit::Pool
- Inherits:
-
Object
- Object
- ThreadKit::Pool
- Defined in:
- lib/thread_kit/pool.rb
Instance Attribute Summary collapse
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
-
#initialize(size) ⇒ Pool
constructor
A new instance of Pool.
-
#schedule(*args, &block) ⇒ Object
schedule a job to be performed.
-
#shutdown ⇒ Object
bail after finishing remaining jobs.
-
#shutdown! ⇒ Object
bail without finishing remaining jobs.
Constructor Details
#initialize(size) ⇒ Pool
Returns a new instance of Pool.
5 6 7 8 9 10 11 |
# File 'lib/thread_kit/pool.rb', line 5 def initialize(size) @size = size @jobs = Queue.new @workers = [] fill_the_pool at_exit { shutdown } end |
Instance Attribute Details
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
3 4 5 |
# File 'lib/thread_kit/pool.rb', line 3 def jobs @jobs end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/thread_kit/pool.rb', line 3 def size @size end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
3 4 5 |
# File 'lib/thread_kit/pool.rb', line 3 def workers @workers end |
Instance Method Details
#schedule(*args, &block) ⇒ Object
schedule a job to be performed
14 15 16 |
# File 'lib/thread_kit/pool.rb', line 14 def schedule(*args, &block) jobs << [block, args] end |
#shutdown ⇒ Object
bail after finishing remaining jobs
19 20 21 22 |
# File 'lib/thread_kit/pool.rb', line 19 def shutdown size.times { exit_worker } workers.map(&:join) end |
#shutdown! ⇒ Object
bail without finishing remaining jobs
25 26 27 28 |
# File 'lib/thread_kit/pool.rb', line 25 def shutdown! jobs.clear shutdown end |