Class: EasyThreadpool::Pool
- Inherits:
-
Object
- Object
- EasyThreadpool::Pool
- Defined in:
- lib/easy_threadpool/pool.rb
Instance Method Summary collapse
-
#initialize(size = 10) ⇒ Pool
constructor
Creating a new ThreadPool of size equal to @size It defines how many threads we will have spawned internally.
-
#process(*args, &block) ⇒ Object
It schedules a job.
-
#shutdown ⇒ Object
Graceful shutdown Always call pool.shutdow to ensure all of your enqueud task are executed.
-
#shutdown! ⇒ Object
Force shutdown.
- #size ⇒ Object
- #task_consumed ⇒ Object
Constructor Details
#initialize(size = 10) ⇒ Pool
Creating a new ThreadPool of size equal to @size It defines how many threads we will have spawned internally.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/easy_threadpool/pool.rb', line 8 def initialize(size = 10) # Pool size @size = size # Queue holds tasks @queue = Queue.new # Threads are created @pool = Array.new(size) { |i| worker i} @consumed = 0 end |
Instance Method Details
#process(*args, &block) ⇒ Object
It schedules a job. The block of code will be executed once a thread is available
21 22 23 |
# File 'lib/easy_threadpool/pool.rb', line 21 def process(*args, &block) @queue << [block, args] end |
#shutdown ⇒ Object
Graceful shutdown Always call pool.shutdow to ensure all of your enqueud task are executed
28 29 30 31 |
# File 'lib/easy_threadpool/pool.rb', line 28 def shutdown 1.upto(@size) {|i| @queue << [:EXIT]} @pool.map(&:join) end |
#shutdown! ⇒ Object
Force shutdown
34 35 36 |
# File 'lib/easy_threadpool/pool.rb', line 34 def shutdown! @pool.map(&:exit) end |
#size ⇒ Object
42 43 44 |
# File 'lib/easy_threadpool/pool.rb', line 42 def size @size end |
#task_consumed ⇒ Object
38 39 40 |
# File 'lib/easy_threadpool/pool.rb', line 38 def task_consumed @consumed end |